package com.cpop.clockin.business.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.cpop.clockin.business.dto.StaffClassesDto;
import com.cpop.clockin.business.entity.*;
import com.cpop.clockin.business.mapper.ClockInStaffMapper;
import com.cpop.clockin.business.service.*;
import com.cpop.clockin.business.vo.ClassesVo;
import com.cpop.clockin.business.vo.StaffClassesVo;
import com.cpop.core.base.entity.PageDomain;
import com.cpop.core.utils.SpringUtils;
import com.cpop.core.utils.sql.SqlUtils;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.row.Db;
import com.mybatisflex.core.row.DbChain;
import com.mybatisflex.core.row.Row;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.cpop.clockin.business.mapper.StaffClassesMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static com.mybatisflex.core.query.QueryMethods.groupConcat;

/**
 * 员工-班次关联表 服务层实现。
 *
 * @author Yxz
 * @since 2024-01-23
 */
@Service("staffClassesService")
public class StaffClassesServiceImpl extends ServiceImpl<StaffClassesMapper, StaffClasses> implements StaffClassesService {
    @Autowired
    private ClockInStaffService clockInStaffService;
    @Autowired
    private ClockInDeptService clockInDeptService;
    @Autowired
    private ClockInStaffMidDeptService clockInStaffMidDeptService;
    @Autowired
    private ClassesService classesService;
    @Autowired
    private ClockInStaffMapper clockInStaffMapper;

    /**
     * 保存员工班次
     *
     * @author: Yxz
     * @date: 2024/1/23 16:59
     * @param: [staffClassesDto]
     * @return: void
     **/
    @Override
    public void saveAll(StaffClassesDto staffClassesDto) {
        this.mapper.deleteByQuery(QueryWrapper.create().eq("staff_id",staffClassesDto.getStaffId()));

        for (String s : staffClassesDto.getClassId()) {
            StaffClasses staffClasses = new StaffClasses();
            staffClasses.setStaffId(staffClassesDto.getStaffId());
            staffClasses.setClassId(s);

            this.save(staffClasses);
        }
    }

    /**
     * 查询所有员工-班次关联表
     *
     * @author: Yxz
     * @date: 2024/1/23 17:50
     * @param: []
     * @return: java.util.List<com.cpop.clockin.business.vo.StaffClassesVo>
     **/
    @Override
    public List<StaffClassesVo> getStaffClassesVolist() {

        List<StaffClassesVo> staffClassesVoList= clockInStaffService.list().stream().map(entity -> {
            StaffClassesVo vo = new StaffClassesVo();
            vo.setStaffName(entity.getName());
            vo.setStaffId(entity.getId());

            // 部门
            String duptName = "";
            for (ClockInStaffMidDept staffMidDept : clockInStaffMidDeptService.list(QueryWrapper.create().eq("staff_id", entity.getId()))) {
                ClockInDept dept = clockInDeptService.getById(staffMidDept.getDeptId());
                duptName = duptName + dept.getName() + " ";
            }
            vo.setDeptName(duptName);

            // 考勤班次字段
            String className = "";
            List<ClassesVo> classesVos = new ArrayList<>();
            for (StaffClasses staffClasses : this.list(QueryWrapper.create().eq("staff_id", entity.getId()))) {
                Classes classes = classesService.getById(staffClasses.getClassId());
                className = className + classes.getClassName() + "";

                ClassesVo classesVO = new ClassesVo();
                BeanUtils.copyProperties(classes,classesVO);

                classesVos.add(classesVO);
            }
            vo.setClassName(className);
            vo.setClassesVoList(classesVos);

            return vo;
        }).collect(Collectors.toList());

        return staffClassesVoList;
    }

    @Override
    public void updateAll(StaffClassesDto staffClassesDto) {
        this.mapper.deleteByQuery(QueryWrapper.create().eq("staff_id",staffClassesDto.getStaffId()));
        this.saveAll(staffClassesDto);
    }

    /**
     * 分页查询
     *
     * @author: Yxz
     * @date: 2024/1/24 11:20
     * @param: [staffName]
     * @return: com.mybatisflex.core.paginate.Page<com.cpop.clockin.business.vo.StaffClassesVo>
     **/
    @Override
    public Page<StaffClassesVo> selectPage(String staffName) {
        PageDomain pageDomain = SqlUtils.getInstance().getPageDomain();

        /*SpringUtils.getBean(ClockInStaffService.class).getMapper().paginateAs(Page.of(pageDomain.getPageSize(),pageDomain.getPageSize()),
                        QueryWrapper.create(),
                        StaffClassesVo.class,
                deptName ->deptName.field(StaffClassesVo::getDeptName)
                        .queryWrapper(vo ->QueryWrapper.create().select(("group_concat(cod.name) as deptName") ).from("cp_oam_dept").as("cod")
                                .leftJoin("cp_oam_staff_mid_dept").as("cosmd").on("cosmd.dept_id = cos.id")
                                .leftJoin("cp_oam_staff").as("cos").on("cos.id = cosmd.staff_id")
                                .where("cos.id = ?",vo.getStaffId())));*/

        /*Page<StaffClassesVo> clockInStaffPage1 = DbChain.table("cp_oam_staff")
                .select("cos.id as staffId","cos.name as staffName").from("cp_oam_staff").as("cos")
                .leftJoin("cp_oam_staff_mid_dept").as("cosmd").on("cosmd.staff_id = cos.id")
                .leftJoin("cp_oam_dept").as("cod").on("cod.id = cosmd.dept_id")
                .like("name", staffName)
                .pageAs(StaffClassesVo.class);

        Db.paginate("cp_oam_staff",pageAs(pageDomain.getPageSize(),pageDomain.getPageSize()),
                QueryWrapper.create(),)*/

        Page<ClockInStaff> clockInStaffPage = clockInStaffMapper.paginate(pageDomain.getPageNum(), pageDomain.getPageSize(), QueryWrapper.create().like("name",staffName));
        List<StaffClassesVo> staffClassesVoList = clockInStaffPage.getRecords().stream().map(entity -> {
            StaffClassesVo vo = new StaffClassesVo();
            vo.setStaffName(entity.getName());
            vo.setStaffId(entity.getId());

            // 部门
            String duptName = "";
            for (ClockInStaffMidDept staffMidDept : clockInStaffMidDeptService.list(QueryWrapper.create().eq("staff_id", entity.getId()))) {
                ClockInDept dept = clockInDeptService.getById(staffMidDept.getDeptId());
                duptName = duptName + dept.getName() + " ";
            }
            vo.setDeptName(duptName);

            // 考勤班次字段
            String className = "";
            List<ClassesVo> classesVos = new ArrayList<>();
            for (StaffClasses staffClasses : this.list(QueryWrapper.create().eq("staff_id", entity.getId()))) {
                Classes classes = classesService.getById(staffClasses.getClassId());
                if (ObjectUtil.isNotNull(classes)) {
                    className = className + classes.getClassName() + " ";
                    ClassesVo classesVO = new ClassesVo();
                    BeanUtils.copyProperties(classes,classesVO);

                    classesVos.add(classesVO);
                }
            }
            vo.setClassName(className);
            vo.setClassesVoList(classesVos);

            return vo;
        }).collect(Collectors.toList());


        Page<StaffClassesVo> staffClassesVoPage = new Page<>(pageDomain.getPageNum(), pageDomain.getPageSize());
        staffClassesVoPage.setRecords(staffClassesVoList);
        staffClassesVoPage.setTotalRow(clockInStaffPage.getTotalRow());
        staffClassesVoPage.setTotalPage(clockInStaffPage.getTotalPage());

        return staffClassesVoPage;
    }
}
