package com.mortals.xhx.module.window.service.impl;
import com.mortals.xhx.common.code.FillStatusEnum;
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.check.service.CheckWindowWorkmanPerformService;
import com.mortals.xhx.module.window.model.WindowWorkmanPerformDetailEntity;
import com.mortals.xhx.module.window.model.WindowWorkmanPerformDetailQuery;
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.model.WindowWorkmanPerformEntity;
import com.mortals.xhx.module.window.service.WindowWorkmanPerformService;
import lombok.extern.slf4j.Slf4j;

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;


    @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());
            });
            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());
            });
            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.setFromName("市政务服务大厅窗口工作人员考核汇总表");
        perform.setSubmitDate(entity.getFillDate());
        perform.setUpdateTime(null);
        perform.setUpdateUserId(null);
        checkWindowWorkmanPerformService.save(perform);
    }
}