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

import com.mortals.framework.model.PageInfo;
import com.mortals.xhx.common.code.AssessmentScopeEnum;
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.PerformPerposeDeptService;
import com.mortals.xhx.module.perform.service.PerformPerposeStaffService;
import com.mortals.xhx.module.staff.model.StaffEntity;
import com.mortals.xhx.module.staff.service.StaffService;
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.PerformPerposeDao;
import com.mortals.xhx.module.perform.service.PerformPerposeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * PerformPerposeService
 * 考核目标 service实现
 *
 * @author zxfei
 * @date 2023-05-16
 */
@Service("performPerposeService")
@Slf4j
public class PerformPerposeServiceImpl extends AbstractCRUDServiceImpl<PerformPerposeDao, PerformPerposeEntity, Long> implements PerformPerposeService {

    @Autowired
    private PerformPerposeStaffService perposeStaffService;
    @Autowired
    private PerformPerposeDeptService perposeDeptService;
    @Autowired
    private DeptService deptService;
    @Autowired
    private StaffService staffService;


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

        list.stream().forEach(item->{
            if(AssessmentScopeEnum.个人.getValue()==item.getAssessmentScope()){
                List<PerformPerposeStaffEntity> staffEntities = perposeStaffService.find(new PerformPerposeStaffQuery().purposeConfId(item.getId()));
                item.setPerposeStaffList(staffEntities);
                item.setStaffPerpose(staffEntities.stream().map(item1->item1.getStaffName()).collect(Collectors.joining(",")));

            }else if(AssessmentScopeEnum.部门.getValue()==item.getAssessmentScope()){
                List<PerformPerposeDeptEntity> deptEntities = perposeDeptService.find(new PerformPerposeDeptQuery().purposeConfId(item.getId()));
                item.setPerposeDeptList(deptEntities);
                item.setDeptPerpose(deptEntities.stream().map(item1->item1.getDeptName()).collect(Collectors.joining(",")));
            }
        });
    }

    @Override
    protected void saveAfter(PerformPerposeEntity entity, Context context) throws AppException {
        super.saveAfter(entity, context);

        if (!ObjectUtils.isEmpty(entity.getPerposeStaffList())) {
            entity.getPerposeStaffList().stream().peek(item -> {
                item.setPurposeConfId(entity.getId());
                if (!ObjectUtils.isEmpty(item.getStaffId())) {
                    StaffEntity staffCache = staffService.getCache(item.getStaffId().toString());
                    if (!ObjectUtils.isEmpty(staffCache)) {
                        item.setStaffName(staffCache.getName());
                        item.setNumber(staffCache.getWorkNum());
                    }
                }
                item.setCreateUserId(this.getContextUserId(context));
                item.setCreateTime(new Date());
            }).count();
            perposeStaffService.save(entity.getPerposeStaffList());
        }
        if (!ObjectUtils.isEmpty(entity.getPerposeDeptList())) {
            entity.getPerposeDeptList().stream().peek(item -> {
                item.setPurposeConfId(entity.getId());
                if (!ObjectUtils.isEmpty(item.getDeptId())) {
                    DeptEntity deptEntity = deptService.get(item.getDeptId(), context);
                    if (!ObjectUtils.isEmpty(deptEntity)) {
                        item.setDeptName(deptEntity.getDeptName());
                    }
                }
                item.setCreateUserId(this.getContextUserId(context));
                item.setCreateTime(new Date());
            }).count();
            perposeDeptService.save(entity.getPerposeDeptList(), context);
        }


    }


    @Override
    protected void updateAfter(PerformPerposeEntity entity, Context context) throws AppException {
        super.updateAfter(entity, context);

        if (!ObjectUtils.isEmpty(entity.getPerposeStaffList())) {
            Long[] performAttendAppealFilesIds = perposeStaffService.find(new PerformPerposeStaffQuery().purposeConfId(entity.getId())).stream().map(PerformPerposeStaffEntity::getId).toArray(Long[]::new);
            perposeStaffService.remove(performAttendAppealFilesIds, context);
            entity.getPerposeStaffList().stream().peek(item -> {
                item.setPurposeConfId(entity.getId());
                if (!ObjectUtils.isEmpty(item.getStaffId())) {
                    StaffEntity staffCache = staffService.getCache(item.getStaffId().toString());
                    if (!ObjectUtils.isEmpty(staffCache)) {
                        item.setStaffName(staffCache.getName());
                        item.setNumber(staffCache.getWorkNum());
                    }
                }
                item.setCreateUserId(this.getContextUserId(context));
                item.setCreateTime(new Date());
            }).count();
            perposeStaffService.save(entity.getPerposeStaffList());
        }
        if (!ObjectUtils.isEmpty(entity.getPerposeDeptList())) {
            Long[] performAttendAppealFilesIds = perposeDeptService.find(new PerformPerposeDeptQuery().purposeConfId(entity.getId())).stream().map(PerformPerposeDeptEntity::getId).toArray(Long[]::new);
            perposeDeptService.remove(performAttendAppealFilesIds, context);
            entity.getPerposeDeptList().stream().peek(item -> {
                item.setPurposeConfId(entity.getId());
                if (!ObjectUtils.isEmpty(item.getDeptId())) {
                    DeptEntity deptEntity = deptService.get(item.getDeptId(), context);
                    if (!ObjectUtils.isEmpty(deptEntity)) {
                        item.setDeptName(deptEntity.getDeptName());
                    }
                }
                item.setCreateUserId(this.getContextUserId(context));
                item.setCreateTime(new Date());
            }).count();
            perposeDeptService.save(entity.getPerposeDeptList(), context);
        }

    }

    @Override
    protected void removeAfter(Long[] ids, Context context, int result) throws AppException {
        super.removeAfter(ids, context, result);

        List<PerformPerposeStaffEntity> staffEntityList = perposeStaffService.find(new PerformPerposeStaffQuery().purposeConfIdList(Arrays.asList(ids)));
        perposeStaffService.removeList(staffEntityList, context);

        List<PerformPerposeDeptEntity> deptEntityList = perposeDeptService.find(new PerformPerposeDeptQuery().purposeConfIdList(Arrays.asList(ids)));
        perposeDeptService.removeList(deptEntityList, context);
    }
}