package com.lz.modules.app.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.lz.common.utils.DateUtils;
import com.lz.common.utils.PageUtils;
import com.lz.common.utils.Query;
import com.lz.modules.app.dto.GraphicsStatisticalDto;
import com.lz.modules.app.dto.StaffBaseInfoDto;
import com.lz.modules.app.dto.StaffDto;
import com.lz.modules.app.dto.StaffStatisticalDto;
import com.lz.modules.app.dao.StaffDao;
import com.lz.modules.app.entity.*;
import com.lz.modules.app.service.*;
import com.lz.modules.job.model.responseBo.DepartmentStaffBo;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;


@Service("staffService")
public class StaffServiceImpl extends ServiceImpl<StaffDao, StaffEntity> implements StaffService {

    protected final static org.slf4j.Logger logger = LoggerFactory.getLogger(StaffServiceImpl.class);

    @Resource
    StaffDao staffDao;
    @Resource
    StaffOccupationService staffOccupationService;
    @Resource
    StaffEducationService staffEducationService;
    @Resource
    StaffProjectExperienceService staffProjectExperienceService;
    @Resource
    StaffWorkTransferRecordService staffWorkTransferRecordService;
    @Resource
    DepartmentsService departmentsService;
    @Resource
    DepartmentsStaffRelateService departmentsStaffRelateService;




    @Override
    public StaffEntity selectStaffById(Long id){
        return staffDao.selectStaffById(id);
    }



    @Override
    public Long insertStaff(StaffEntity staff){
        return staffDao.insertStaff(staff);
    }



    @Override
    public int updateStaffById(StaffEntity staff){
        return staffDao.updateStaffById(staff);
    }



    @Override
    public int updateCoverStaffById(StaffEntity staff){
        return staffDao.updateCoverStaffById(staff);
    }



    @Override
    public int deleteStaffById(Long id){
        return staffDao.deleteStaffById(id);
    }

    @Override
    public StaffEntity selectByPhone(String phone) {
        return staffDao.selectByPhone(phone);
    }


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<StaffEntity> page = this.page(
                new Query<StaffEntity>().getPage(params),
                new QueryWrapper<StaffEntity>()
        );
        return new PageUtils(page);
    }

    @Override
    public List<StaffEntity> updateStaffsInfo(List<DepartmentStaffBo> staffs) {
        try {
            List<StaffEntity> staffEntitys = Lists.newArrayList();
            for (DepartmentStaffBo staffBo : staffs) {
                StaffEntity staffEntity = staffDao.getStaffInfoByOpenId(staffBo.getOpenId());
                StaffEntity staff = convertStaffEntity(staffBo);
                if (staffEntity != null) {
                    staff.setId(staffEntity.getId());
                    staffDao.updateStaff(staff);
                } else {
                    staffDao.addStaff(staff);

                }
                staffEntitys.add(staff);
            }
            return staffEntitys;
        } catch (Exception e) {
            logger.info("updateDepartmentInfos error : " + e);
            return null;
        }
    }

    @Override
    public StaffEntity getStaffInfoByOpenId(String openId) {
        return staffDao.getStaffInfoByOpenId(openId);
    }

    @Override
    public StaffEntity getByName(String name) {
        return staffDao.getByName(name);
    }

    @Override
    public List<StaffDto> getStaffInfos(int currentPage, int limit, String departmentId, String name, Integer intoStaffStatus) {
        Integer staffStatus = 0;
        if (intoStaffStatus == 1) {
            staffStatus = intoStaffStatus;
        }
        List<StaffDto> staffInfos = staffDao.getStaffInfos(departmentId, name, staffStatus, limit, (currentPage - 1) * limit);

        return staffInfos;
    }

    @Override
    public int getStaffInfoCount(String departmentId, String name, Integer intoStaffStatus) {
        Integer staffStatus = 0;
        if (intoStaffStatus == 1) {
            staffStatus = intoStaffStatus;
        }
        return staffDao.getStaffInfoCount(departmentId, name, staffStatus);
    }

    @Override
    public StaffBaseInfoDto getBaseInfo(Long staffId) {
        return staffDao.getBaseInfo(staffId);
    }

    @Override
    public StaffStatisticalDto getStatisticalData(String departmentId, String beginDate, String endDate) {
        return staffDao.getStatisticalData(departmentId, beginDate, endDate);
    }

    @Override
    public List<GraphicsStatisticalDto> getGenderData(String departmentId, String beginDate, String endDate) {
        List<GraphicsStatisticalDto> graphicsStatisticals = staffDao.getGenderData(departmentId, beginDate, endDate);
        BigDecimal totalCount = BigDecimal.ZERO;
        for (GraphicsStatisticalDto graphicsStatisticalDto : graphicsStatisticals) {
            totalCount = totalCount.add(new BigDecimal(graphicsStatisticalDto.getNumber()));
        }
        for (GraphicsStatisticalDto graphicsStatisticalDto : graphicsStatisticals) {
            graphicsStatisticalDto.setRate(new BigDecimal(graphicsStatisticalDto.getNumber() * 100).divide(totalCount, 2, BigDecimal.ROUND_HALF_UP) + "%");
        }
        return graphicsStatisticals;
    }

    @Override
    public List<GraphicsStatisticalDto> getAgeData(String departmentId, String beginDate, String endDate) {
        List<GraphicsStatisticalDto> graphicsStatisticals = staffDao.getAgeData(departmentId, beginDate, endDate);
        BigDecimal totalCount = BigDecimal.ZERO;
        for (GraphicsStatisticalDto graphicsStatisticalDto : graphicsStatisticals) {
            totalCount = totalCount.add(new BigDecimal(graphicsStatisticalDto.getNumber()));
        }
        for (GraphicsStatisticalDto graphicsStatisticalDto : graphicsStatisticals) {
            graphicsStatisticalDto.setRate(new BigDecimal(graphicsStatisticalDto.getNumber() * 100).divide(totalCount, 2, BigDecimal.ROUND_HALF_UP) + "%");
        }
        return graphicsStatisticals;
    }

    @Override
    public List<GraphicsStatisticalDto> getJobSeniorityData(String departmentId, String beginDate, String endDate) {
        List<GraphicsStatisticalDto> graphicsStatisticals = staffDao.getJobSeniorityData(departmentId, beginDate, endDate);
        BigDecimal totalCount = BigDecimal.ZERO;
        for (GraphicsStatisticalDto graphicsStatisticalDto : graphicsStatisticals) {
            totalCount = totalCount.add(new BigDecimal(graphicsStatisticalDto.getNumber()));
        }
        for (GraphicsStatisticalDto graphicsStatisticalDto : graphicsStatisticals) {
            graphicsStatisticalDto.setRate(new BigDecimal(graphicsStatisticalDto.getNumber() * 100).divide(totalCount, 2, BigDecimal.ROUND_HALF_UP) + "%");
        }
        return graphicsStatisticals;
    }

    @Override
    public List<GraphicsStatisticalDto> getEducationData(String departmentId, String beginDate, String endDate) {
        List<GraphicsStatisticalDto> graphicsStatisticals = staffDao.getEducationData(departmentId, beginDate, endDate);
        BigDecimal totalCount = BigDecimal.ZERO;
        for (GraphicsStatisticalDto graphicsStatisticalDto : graphicsStatisticals) {
            totalCount = totalCount.add(new BigDecimal(graphicsStatisticalDto.getNumber()));
        }
        for (GraphicsStatisticalDto graphicsStatisticalDto : graphicsStatisticals) {
            graphicsStatisticalDto.setRate(new BigDecimal(graphicsStatisticalDto.getNumber() * 100).divide(totalCount, 2, BigDecimal.ROUND_HALF_UP) + "%");
        }
        return graphicsStatisticals;
    }

    @Override
    public void enterDatabase(List<Map<String, String>> sheet12List, List<Map<String, String>> sheet3List, List<Map<String, String>> sheet4List) {
        for (Map<String, String> map : sheet12List) {
            StaffEntity staffEntity = JSON.parseObject(JSON.toJSONString(map), StaffEntity.class);
            StaffOccupationEntity occupationEntity = JSON.parseObject(JSON.toJSONString(map), StaffOccupationEntity.class);
            StaffEducationEntity educationEntity = JSON.parseObject(JSON.toJSONString(map), StaffEducationEntity.class);
            StaffEntity Staff = this.getByName(staffEntity.getName());
            if (Staff == null) {
                this.save(staffEntity);
            } else {
                staffEntity.setId(Staff.getId());
                this.updateById(staffEntity);
            }

            createDepartmentInfo(staffEntity, map.get("primarySector"));
            createDepartmentInfo(staffEntity, map.get("secondarySector"));
            createDepartmentInfo(staffEntity, map.get("tertiarySector"));

            StaffOccupationEntity occupation = staffOccupationService.getOne(new QueryWrapper<StaffOccupationEntity>().eq("staff_id", staffEntity.getId()));
            if (occupation == null) {
                occupationEntity.setStaffId(staffEntity.getId());
                staffOccupationService.save(occupationEntity);
            } else {
                occupationEntity.setId(occupation.getId());
                staffOccupationService.updateById(occupationEntity);
            }

            StaffEducationEntity education = staffEducationService.getOne(new QueryWrapper<StaffEducationEntity>().eq("staff_id", staffEntity.getId()));
            if (education == null) {
                educationEntity.setStaffId(staffEntity.getId());
                staffEducationService.save(educationEntity);
            } else {
                educationEntity.setId(education.getId());
                staffEducationService.updateById(educationEntity);
            }
        }
        for (Map<String, String> map : sheet3List) {
            StaffEntity Staff = this.getByName(map.get("name"));
            if (Staff != null) {
                staffProjectExperienceService.update(new UpdateWrapper<StaffProjectExperienceEntity>().set("is_delete", 1).eq("staff_id", Staff.getId()));
            }
        }
        for (Map<String, String> map : sheet3List) {
            if (map.get("projectBeginTime").length() == 6) {
                map.put("projectBeginTime", map.get("projectBeginTime") + "01");
            }
            if (map.get("projectEndTime").length() == 6) {
                map.put("projectEndTime", map.get("projectEndTime") + "01");
            }
            StaffProjectExperienceEntity staffProjectExperienceEntity = JSON.parseObject(JSON.toJSONString(map), StaffProjectExperienceEntity.class);
            StaffEntity Staff = this.getByName(map.get("name"));
            if (Staff == null) {
                continue;
            } else {
                StaffProjectExperienceEntity projectExperienceEntity = staffProjectExperienceService.getOne(new QueryWrapper<StaffProjectExperienceEntity>().eq("staff_id", Staff.getId()).eq("project_name", staffProjectExperienceEntity.getProjectName()));
                if (projectExperienceEntity == null) {
                    staffProjectExperienceEntity.setStaffId(Staff.getId());
                    staffProjectExperienceService.save(staffProjectExperienceEntity);
                } else {
                    logger.info(DateUtils.format(staffProjectExperienceEntity.getProjectBeginTime(), DateUtils.DATE_TIME_PATTERN));
                    logger.info(DateUtils.format(staffProjectExperienceEntity.getProjectEndTime(), DateUtils.DATE_TIME_PATTERN));
                    staffProjectExperienceEntity.setId(projectExperienceEntity.getId());
                    staffProjectExperienceEntity.setIsDelete(0);
                    staffProjectExperienceService.updateById(staffProjectExperienceEntity);
                }
            }
        }
        for (Map<String, String> map : sheet4List) {
            StaffEntity Staff = this.getByName(map.get("name"));
            if (Staff != null) {
                staffWorkTransferRecordService.update(new UpdateWrapper<StaffWorkTransferRecordEntity>().set("is_delete", 1).eq("staff_id", Staff.getId()));
            }
        }
        for (Map<String, String> map : sheet4List) {
            if (map.get("transferTime").length() == 6) {
                map.put("transferTime", map.get("transferTime") + "01");
            }
            StaffWorkTransferRecordEntity staffWorkTransferRecordEntity = JSON.parseObject(JSON.toJSONString(map), StaffWorkTransferRecordEntity.class);
            StaffEntity Staff = this.getByName(map.get("name"));
            if (Staff == null) {
                continue;
            } else {
                StaffWorkTransferRecordEntity workTransferRecordEntity = staffWorkTransferRecordService.getOne(new QueryWrapper<StaffWorkTransferRecordEntity>().eq("staff_id", Staff.getId()).eq("transfer_time", staffWorkTransferRecordEntity.getTransferTime()));
                if (workTransferRecordEntity == null) {
                    staffWorkTransferRecordEntity.setStaffId(Staff.getId());
                    staffWorkTransferRecordService.save(staffWorkTransferRecordEntity);
                } else {
                    staffWorkTransferRecordEntity.setId(workTransferRecordEntity.getId());
                    staffWorkTransferRecordEntity.setIsDelete(0);
                    staffWorkTransferRecordService.updateById(staffWorkTransferRecordEntity);
                }
            }
        }
    }

    @Override
    public List<StaffEntity> selectByName(String name) {
        return staffDao.selectByName(name);
    }

    @Override
    public List<StaffEntity> selectByIds(List<Long> staffIds) {

        return staffDao.selectByIds(staffIds);
    }

    private void createDepartmentInfo(StaffEntity staffEntity, String departmentName) {
        DepartmentsEntity departmentsEntity = departmentsService.getOne(new QueryWrapper<DepartmentsEntity>().eq("department_name", departmentName));
        if (departmentsEntity != null) {
            DepartmentsStaffRelateEntity departmentsStaffRelateEntity = departmentsStaffRelateService.getOne(new QueryWrapper<DepartmentsStaffRelateEntity>().eq("department_id", departmentsEntity.getDepartmentId()).eq("staff_id", staffEntity.getId()));
            if (departmentsStaffRelateEntity == null) {
                departmentsStaffRelateEntity = new DepartmentsStaffRelateEntity();
                departmentsStaffRelateEntity.setStaffId(staffEntity.getId());
                departmentsStaffRelateEntity.setDepartmentId(departmentsEntity.getDepartmentId());
                departmentsStaffRelateService.save(departmentsStaffRelateEntity);
            }
        }
    }

    private StaffEntity convertStaffEntity(DepartmentStaffBo staffBo) {
        StaffEntity staffEntity = new StaffEntity();
        staffEntity.setName(staffBo.getName());//员工姓名
        staffEntity.setGender(staffBo.getGender());
        staffEntity.setMobile(staffBo.getMobile());
        staffEntity.setEmail(staffBo.getEmail());
        staffEntity.setOpenId(staffBo.getOpenId());
        staffEntity.setEmployeeId(staffBo.getEmployeeId());
        staffEntity.setUnionId(staffBo.getUnionId());
        staffEntity.setIsLeader(staffBo.getIsLeader());
        staffEntity.setAvatar(staffBo.getAvatar());
        staffEntity.setJobNumber(staffBo.getEmployeeNo());
        return staffEntity;
    }

}
