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.*;
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.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 saveBefore(WindowPerformEntity entity, Context context) throws AppException {
        super.saveBefore(entity, context);
        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.setFromName("市政务服务大厅窗口考核登记表");
        perform.setSubmitDate(entity.getFillDate());
        perform.setUpdateTime(null);
        perform.setUpdateUserId(null);
        checkWindowPerformService.save(perform);
    }
}