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.YesNoEnum;
import com.mortals.xhx.common.pdu.window.WindowPdu;
import com.mortals.xhx.common.utils.BeanUtil;
import com.mortals.xhx.feign.window.IWindowFeign;
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.check.service.CheckWindowWorkmanPerformService;
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.service.WindowOwnerDetailService;
import com.mortals.xhx.module.window.service.WindowWorkmanPerformDetailService;
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.WindowWorkmanPerformDao;
import com.mortals.xhx.module.window.service.WindowWorkmanPerformService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;

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

/**
 * WindowWorkmanPerformService
 * 窗口人员考核汇总登记 service实现
 *
 * @author zxfei
 * @date 2024-01-17
 */
@Service("windowWorkmanPerformService")
@Slf4j
public class WindowWorkmanPerformServiceImpl extends AbstractCRUDServiceImpl<WindowWorkmanPerformDao, WindowWorkmanPerformEntity, Long> implements WindowWorkmanPerformService {

    @Autowired
    private WindowWorkmanPerformDetailService windowWorkmanPerformDetailService;
    @Autowired
    private CheckWindowWorkmanPerformService checkWindowWorkmanPerformService;
    @Autowired
    private StaffService staffService;
    @Autowired
    private WindowOwnerDetailService windowOwnerDetailService;

    @Autowired
    private IWindowFeign windowFeign;


    @Override
    protected void saveBefore(WindowWorkmanPerformEntity entity, Context context) throws AppException {
        //buildWindowInfo(entity);
        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());
//            }
//        }
        super.saveBefore(entity, context);
    }

    private void buildWindowInfo(WindowWorkmanPerformEntity entity) {
        if (!ObjectUtils.isEmpty(entity.getWindowId())) {
            Rest<WindowPdu> rest = windowFeign.info(entity.getWindowId());
            if (YesNoEnum.YES.getValue() == rest.getCode()) {
                entity.setWindowName(rest.getData().getName());
                entity.setWindowCode(rest.getData().getFromnum());
            }
        }
    }

    @Override
    protected void updateBefore(WindowWorkmanPerformEntity entity, Context context) throws AppException {
        //buildWindowInfo(entity);
        super.updateBefore(entity, context);
    }

    @Override
    protected void saveAfter(WindowWorkmanPerformEntity entity, Context context) throws AppException {
        if (CollectionUtils.isNotEmpty(entity.getWorkmanPerformDetailList())) {
            entity.getWorkmanPerformDetailList().forEach(item -> {
                item.setPerformId(entity.getId());
                item.setCreateUserId(this.getContextUserId(context));
                item.setCreateTime(new Date());
                StaffEntity staff = staffService.get(item.getStaffId());
                if(staff!=null){
                    item.setDeptId(staff.getDeptId());
                    item.setDeptName(staff.getDeptName());
                    item.setSalaId(staff.getSalaId());
                    item.setSalaName(staff.getSalaName());
                    item.setYear(entity.getYear());
                    item.setMonth(entity.getMonth());
                }
            });
            windowWorkmanPerformDetailService.save(entity.getWorkmanPerformDetailList());
        }
        if (entity.getFillStatus() == FillStatusEnum.提交.getValue()) {
            saveToCheck(entity);
        }
        super.saveAfter(entity, context);
    }

    @Override
    protected void updateAfter(WindowWorkmanPerformEntity entity, Context context) throws AppException {
        if (CollectionUtils.isNotEmpty(entity.getWorkmanPerformDetailList())) {
            Long[] detailList = windowWorkmanPerformDetailService.find(new WindowWorkmanPerformDetailQuery().performId(entity.getId())).stream().map(WindowWorkmanPerformDetailEntity::getId).toArray(Long[]::new);
            windowWorkmanPerformDetailService.remove(detailList, context);
            entity.getWorkmanPerformDetailList().forEach(item -> {
                item.setPerformId(entity.getId());
                item.setCreateUserId(this.getContextUserId(context));
                item.setCreateTime(new Date());
                item.setUpdateUserId(this.getContextUserId(context));
                item.setUpdateTime(new Date());
                StaffEntity staff = staffService.get(item.getStaffId());
                if(staff!=null){
                    item.setDeptId(staff.getDeptId());
                    item.setDeptName(staff.getDeptName());
                    item.setSalaId(staff.getSalaId());
                    item.setSalaName(staff.getSalaName());
                    item.setYear(entity.getYear());
                    item.setMonth(entity.getMonth());
                }
            });
            windowWorkmanPerformDetailService.save(entity.getWorkmanPerformDetailList());
        }
        if (entity.getFillStatus() == FillStatusEnum.提交.getValue()) {
            saveToCheck(entity);
        }
        super.updateAfter(entity, context);
    }

    @Override
    protected void removeAfter(Long[] ids, Context context, int result) throws AppException {
        List<WindowWorkmanPerformDetailEntity> detailEntities = windowWorkmanPerformDetailService.find(new WindowWorkmanPerformDetailQuery().performIdList(Arrays.asList(ids)));
        windowWorkmanPerformDetailService.removeList(detailEntities, context);
        List<CheckWindowWorkmanPerformEntity> performEntitys = checkWindowWorkmanPerformService.find(new CheckWindowWorkmanPerformQuery().recordIdList(Arrays.asList(ids)));
        checkWindowWorkmanPerformService.removeList(performEntitys, context);
        super.removeAfter(ids, context, result);
    }

    private void saveToCheck(WindowWorkmanPerformEntity entity) {
        CheckWindowWorkmanPerformEntity perform = new CheckWindowWorkmanPerformEntity();
        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);
        checkWindowWorkmanPerformService.save(perform);
    }
}