package com.mortals.xhx.module.perform.service.impl;

import com.mortals.framework.model.PageInfo;
import com.mortals.xhx.base.system.user.model.UserEntity;
import com.mortals.xhx.base.system.user.model.UserQuery;
import com.mortals.xhx.base.system.user.service.UserService;
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.*;
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.service.PerformDeptConfService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;

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;
    @Autowired
    private UserService userService;

    @Override
    protected void findAfter(PerformDeptConfEntity params, PageInfo pageInfo, Context context, List<PerformDeptConfEntity> list) throws AppException {

        Map<Long, UserEntity> userMap = userService.findToMap(new UserQuery(), context);
        if (CollectionUtils.isNotEmpty(list)) {
            for (PerformDeptConfEntity item : list) {
                UserEntity userEntity = userMap.get(item.getCreateUserId());
                if (!ObjectUtils.isEmpty(userEntity)) {
                    item.setCreateUserName(userEntity.getRealName());
                }
                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());
        }
        updateCount(entity);
    }

    @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);
        }else {
            List<PerformDeptConfDetailEntity> list = performDeptConfDetailService.find(new PerformDeptConfDetailQuery().deptConfId(entity.getId()));
            if(CollectionUtils.isNotEmpty(list)){
                Long[] ids = list.stream().map(PerformDeptConfDetailEntity::getId).toArray(Long[]::new);
                performDeptConfDetailService.remove(ids,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());
        }
        updateCount(entity);
    }

    private void updateCount(PerformDeptConfEntity entity) {
        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);
            }
        }else {
            entity.setAttendCount(0);
            entity.setAssessCount(0);
            entity.setWorkCount(0);
            entity.setOtherCount(0);
            entity.setEffectCount(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);
        }else {
            List<PerformDeptConfDetailEntity> list = performDeptConfDetailService.find(new PerformDeptConfDetailQuery().deptConfId(entity.getId()));
            if(CollectionUtils.isNotEmpty(list)){
                Long[] ids = list.stream().map(PerformDeptConfDetailEntity::getId).toArray(Long[]::new);
                performDeptConfDetailService.remove(ids,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);
            }
        }
    }
}