package com.mortals.xhx.module.perform.service.impl;
import com.mortals.framework.model.PageInfo;
import com.mortals.xhx.common.code.CheckTypeEnum;
import com.mortals.xhx.module.dept.model.DeptEntity;
import com.mortals.xhx.module.dept.service.DeptService;
import com.mortals.xhx.module.perform.model.PerformDeptConfDetailEntity;
import com.mortals.xhx.module.perform.model.PerformDeptConfDetailQuery;
import com.mortals.xhx.module.perform.model.PerformRulesCategoryEntity;
import com.mortals.xhx.module.perform.service.PerformDeptConfDetailService;
import com.mortals.xhx.module.perform.service.PerformRulesCategoryService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.xhx.module.perform.dao.PerformDeptConfDao;
import com.mortals.xhx.module.perform.model.PerformDeptConfEntity;
import com.mortals.xhx.module.perform.service.PerformDeptConfService;
import lombok.extern.slf4j.Slf4j;

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

/**
* PerformDeptConfService
* 部门自动考核信息 service实现
*
* @author zxfei
* @date 2023-05-16
*/
@Service("performDeptConfService")
@Slf4j
public class PerformDeptConfServiceImpl extends AbstractCRUDServiceImpl<PerformDeptConfDao, PerformDeptConfEntity, Long> implements PerformDeptConfService {

    @Autowired
    private PerformDeptConfDetailService performDeptConfDetailService;
    @Autowired
    private DeptService deptService;
    @Autowired
    private PerformRulesCategoryService performRulesCategoryService;

    @Override
    protected void findAfter(PerformDeptConfEntity params, PageInfo pageInfo, Context context, List<PerformDeptConfEntity> list) throws AppException {
        if(CollectionUtils.isNotEmpty(list)){
            for (PerformDeptConfEntity item:list){
                List<PerformDeptConfDetailEntity> detailEntityList = performDeptConfDetailService.find(new PerformDeptConfDetailQuery().deptConfId(item.getId()));
                item.setDeptConfDetail(detailEntityList);
            }
        }
    }

    @Override
    protected void updateBefore(PerformDeptConfEntity entity, Context context) throws AppException {
        super.updateBefore(entity, context);
        DeptEntity deptEntity = deptService.get(entity.getDeptId());
        if (deptEntity != null) {
            entity.setDeptName(deptEntity.getDeptName());
        }
    }

    @Override
    protected void updateAfter(PerformDeptConfEntity entity, Context context) throws AppException {
        if(CollectionUtils.isNotEmpty(entity.getDeptConfDetail())){
            Long[] detailIds = performDeptConfDetailService.find(new PerformDeptConfDetailQuery().deptConfId(entity.getId())).stream().map(PerformDeptConfDetailEntity::getId).toArray(Long[]::new);
            performDeptConfDetailService.remove(detailIds,context);
            for(PerformDeptConfDetailEntity item:entity.getDeptConfDetail()){
                item.setDeptConfId(entity.getId());
                item.setCreateTime(entity.getCreateTime());
                item.setCreateUser(entity.getCreateUser());
                item.setDeptId(entity.getDeptId());
                item.setDeptName(entity.getDeptName());
                PerformRulesCategoryEntity categoryEntity = performRulesCategoryService.get(item.getCategoryId());
                if(categoryEntity!=null){
                    item.setCategoryName(categoryEntity.getName());
                }
                item.setId(null);
            }
            performDeptConfDetailService.save(entity.getDeptConfDetail(),context);
        }
    }

    @Override
    protected void saveBefore(PerformDeptConfEntity entity, Context context) throws AppException {
        super.saveBefore(entity,context);
        DeptEntity deptEntity = deptService.get(entity.getDeptId());
        if(deptEntity!=null){
            entity.setDeptName(deptEntity.getDeptName());
        }
        if(CollectionUtils.isNotEmpty(entity.getDeptConfDetail())){
            Map<Integer,List<PerformDeptConfDetailEntity>> groupMap = entity.getDeptConfDetail().stream().collect(Collectors.groupingBy(PerformDeptConfDetailEntity::getType));
            if(groupMap.containsKey(CheckTypeEnum.考勤绩效.getValue())) {
                entity.setAttendCount(groupMap.get(CheckTypeEnum.考勤绩效.getValue()).size());
            }else {
                entity.setAttendCount(0);
            }
            if(groupMap.containsKey(CheckTypeEnum.评价绩效.getValue())) {
                entity.setAssessCount(groupMap.get(CheckTypeEnum.评价绩效.getValue()).size());
            }else {
                entity.setAssessCount(0);
            }
            if(groupMap.containsKey(CheckTypeEnum.办件绩效.getValue())) {
                entity.setWorkCount(groupMap.get(CheckTypeEnum.办件绩效.getValue()).size());
            }else {
                entity.setWorkCount(0);
            }
            if(groupMap.containsKey(CheckTypeEnum.效能绩效.getValue())) {
                entity.setEffectCount(groupMap.get(CheckTypeEnum.效能绩效.getValue()).size());
            }else {
                entity.setEffectCount(0);
            }
            if(groupMap.containsKey(CheckTypeEnum.其它绩效.getValue())) {
                entity.setOtherCount(groupMap.get(CheckTypeEnum.其它绩效.getValue()).size());
            }else {
                entity.setOtherCount(0);
            }
        }
    }

    @Override
    protected void saveAfter(PerformDeptConfEntity entity, Context context) throws AppException {
        if(CollectionUtils.isNotEmpty(entity.getDeptConfDetail())){
            for(PerformDeptConfDetailEntity item:entity.getDeptConfDetail()){
                item.setDeptConfId(entity.getId());
                item.setCreateTime(entity.getCreateTime());
                item.setCreateUser(entity.getCreateUser());
                item.setDeptId(entity.getDeptId());
                item.setDeptName(entity.getDeptName());
                PerformRulesCategoryEntity categoryEntity = performRulesCategoryService.get(item.getCategoryId());
                if(categoryEntity!=null){
                    item.setCategoryName(categoryEntity.getName());
                }
                item.setId(null);
            }
            performDeptConfDetailService.save(entity.getDeptConfDetail(),context);
        }
    }

    @Override
    public PerformDeptConfEntity get(Long key, Context context) throws AppException {
        PerformDeptConfEntity entity = this.dao.get(key);
        if(entity!=null) {
            List<PerformDeptConfDetailEntity> detailEntityList = performDeptConfDetailService.find(new PerformDeptConfDetailQuery().deptConfId(entity.getId()));
            entity.setDeptConfDetail(detailEntityList);
        }
        return entity;
    }

    @Override
    protected void removeAfter(Long[] ids, Context context, int result) throws AppException {
        if(ids != null && ids.length != 0){
            for (Long id:ids){
                PerformDeptConfDetailEntity condition = new PerformDeptConfDetailEntity();
                condition.setDeptConfId(id);
                performDeptConfDetailService.remove(condition,context);
            }
        }
    }
}