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

import com.mortals.framework.common.Rest;
import com.mortals.xhx.common.code.FillStatusEnum;
import com.mortals.xhx.common.code.WorkmanPerformTypeEnum;
import com.mortals.xhx.common.utils.BeanUtil;
import com.mortals.xhx.module.check.model.CheckWindowPerformEntity;
import com.mortals.xhx.module.check.model.CheckWindowPerformQuery;
import com.mortals.xhx.module.check.model.CheckWindowWorkmanPerformEntity;
import com.mortals.xhx.module.check.model.CheckWindowWorkmanPerformQuery;
import com.mortals.xhx.module.check.service.CheckWindowPerformService;
import com.mortals.xhx.module.staff.model.StaffEntity;
import com.mortals.xhx.module.staff.service.StaffService;
import com.mortals.xhx.module.window.model.*;
import com.mortals.xhx.module.window.model.vo.WindowPerformAllVo;
import com.mortals.xhx.module.window.service.WindowOwnerDetailService;
import com.mortals.xhx.module.window.service.WindowWorkmanPerformService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
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.window.dao.WindowPerformDao;
import com.mortals.xhx.module.window.service.WindowPerformService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * WindowPerformService
 * 大厅窗口信息 service实现
 *
 * @author zxfei
 * @date 2024-01-17
 */
@Service("windowPerformService")
@Slf4j
public class WindowPerformServiceImpl extends AbstractCRUDServiceImpl<WindowPerformDao, WindowPerformEntity, Long> implements WindowPerformService {

    @Autowired
    private CheckWindowPerformService checkWindowPerformService;
    @Autowired
    private WindowWorkmanPerformService windowWorkmanPerformService;
    @Autowired
    private StaffService staffService;
    @Autowired
    private WindowOwnerDetailService windowOwnerDetailService;


    @Override
    protected void saveBefore(WindowPerformEntity entity, Context context) throws AppException {
        super.saveBefore(entity, context);
        StaffEntity staff = staffService.get(entity.getOwnerId());
        if(staff!=null){
            entity.setDeptId(staff.getDeptId());
            entity.setDeptName(staff.getDeptName());
            entity.setSalaId(staff.getSalaId());
            entity.setSalaName(staff.getSalaName());
        }
        if(entity.getWindowId()!=null) {
            WindowOwnerDetailEntity windowOwnerDetailEntity = windowOwnerDetailService.selectOne(new WindowOwnerDetailQuery().windowId(entity.getWindowId()));
            if(windowOwnerDetailEntity!=null){
                entity.setWindowCode(windowOwnerDetailEntity.getWindowCode());
                entity.setWindowName(windowOwnerDetailEntity.getWindowName());
            }
        }
        deleteExistBill(entity, context);
    }

    private void deleteExistBill(WindowPerformEntity entity, Context context) {
        WindowPerformEntity windowPerformEntity = this.selectOne(new WindowPerformQuery()
                .year(entity.getYear())
                .month(entity.getMonth())
                .windowId(entity.getWindowId())
                .fillStatus(FillStatusEnum.提交.getValue()));
        if (!ObjectUtils.isEmpty(windowPerformEntity) && entity.getFillStatus() == FillStatusEnum.提交.getValue()) {
            this.remove(Arrays.asList(windowPerformEntity.getId()), context);

            //删除

        }
    }


    @Override
    protected void updateBefore(WindowPerformEntity entity, Context context) throws AppException {
        super.updateBefore(entity, context);
        deleteExistBill(entity, context);
    }

    @Override
    protected void saveAfter(WindowPerformEntity entity, Context context) throws AppException {
        //对同一个窗口的同一时间只能有一个窗口进行操作

        if (entity.getFillStatus() == FillStatusEnum.提交.getValue()) {
            saveToCheck(entity);
        }
        super.saveAfter(entity, context);
    }

    @Override
    protected void updateAfter(WindowPerformEntity entity, Context context) throws AppException {
        if (entity.getFillStatus() == FillStatusEnum.提交.getValue()) {
            saveToCheck(entity);
        }
        super.updateAfter(entity, context);
    }

    @Override
    protected void removeAfter(Long[] ids, Context context, int result) throws AppException {
        List<CheckWindowPerformEntity> performEntitys = checkWindowPerformService.find(new CheckWindowPerformQuery().recordIdList(Arrays.asList(ids)));
        checkWindowPerformService.removeList(performEntitys, context);
        super.removeAfter(ids, context, result);
    }

    private void saveToCheck(WindowPerformEntity entity) {
        CheckWindowPerformEntity perform = new CheckWindowPerformEntity();
        perform.initAttrValue();
        BeanUtils.copyProperties(entity, perform, BeanUtil.getNullPropertyNames(entity));
        perform.setId(null);
        perform.setRecordId(entity.getId());
        perform.setWindowName(entity.getWindowName()+"("+entity.getWindowCode()+")");
        perform.setFromName("市政务服务大厅窗口考核登记表");
        perform.setSubmitDate(entity.getFillDate());
        perform.setUpdateTime(null);
        perform.setUpdateUserId(null);
        checkWindowPerformService.save(perform);
    }

    @Override
    public Rest<List<WindowPerformAllVo>> getAllList(WindowPerformQuery query, Context context) {
        List<WindowPerformAllVo> allList = new ArrayList<>();
        query.setOwnerId(context.getUser().getCustomerId());
        List<WindowPerformEntity> windowPerformEntities = this.find(query, context);
        if (!ObjectUtils.isEmpty(windowPerformEntities)) {
            List<WindowPerformAllVo> collect = windowPerformEntities.stream().map(item -> {
                WindowPerformAllVo windowPerformAllVo = new WindowPerformAllVo();
                BeanUtils.copyProperties(item, windowPerformAllVo, BeanUtil.getNullPropertyNames(item));
                windowPerformAllVo.setFormName(WorkmanPerformTypeEnum.市政务服务大厅窗口考核登记表.getDesc());
                windowPerformAllVo.setFormType(WorkmanPerformTypeEnum.市政务服务大厅窗口考核登记表.getValue());
                return windowPerformAllVo;
            }).collect(Collectors.toList());
            allList.addAll(collect);
        }

        WindowWorkmanPerformQuery windowWorkmanPerformQuery = new WindowWorkmanPerformQuery();
        windowWorkmanPerformQuery.setOwnerId(context.getUser().getCustomerId());
        windowWorkmanPerformQuery.setYear(query.getYear());
        windowWorkmanPerformQuery.setMonth(query.getMonth());
        windowWorkmanPerformQuery.setWindowCode(query.getWindowCode());
        windowWorkmanPerformQuery.setWindowName(query.getWindowName());

        List<WindowWorkmanPerformEntity> windowWorkmanPerformEntities = windowWorkmanPerformService.find(windowWorkmanPerformQuery);
        if (!ObjectUtils.isEmpty(windowWorkmanPerformEntities)) {
            List<WindowPerformAllVo> collect = windowWorkmanPerformEntities.stream().map(item -> {
                WindowPerformAllVo windowPerformAllVo = new WindowPerformAllVo();
                BeanUtils.copyProperties(item, windowPerformAllVo, BeanUtil.getNullPropertyNames(item));
                windowPerformAllVo.setFormName(WorkmanPerformTypeEnum.市政务服务大厅窗口工作人员考核汇总表.getDesc());
                windowPerformAllVo.setFormType(WorkmanPerformTypeEnum.市政务服务大厅窗口工作人员考核汇总表.getValue());
                return windowPerformAllVo;
            }).collect(Collectors.toList());
            allList.addAll(collect);
        }

       allList = allList.stream().sorted(Comparator.comparing(WindowPerformAllVo::getFillDate).reversed()).collect(Collectors.toList());


        return Rest.ok(allList);
    }
}