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.window.model.WindowWorkmanPerformDetailEntity;
import com.mortals.xhx.module.window.model.WindowWorkmanPerformDetailQuery;
import com.mortals.xhx.module.window.model.WindowWorkmanPerformEntity;
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.model.WindowPerformEntity;
import com.mortals.xhx.module.window.service.WindowPerformService;
import lombok.extern.slf4j.Slf4j;

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

/**
* 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;

    @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.setFromName("市政务服务大厅窗口考核登记表");
        perform.setSubmitDate(entity.getFillDate());
        perform.setUpdateTime(null);
        perform.setUpdateUserId(null);
        checkWindowPerformService.save(perform);
    }
}