package cn.ezhizao.project.business.workOrder.service.impl;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.*;
import java.util.stream.Collectors;
import javax.annotation.Resource;

import cn.ezhizao.common.utils.SecurityUtils;
import cn.ezhizao.project.business.entrust.mapper.BizEntrustMapper;
import cn.ezhizao.project.business.order.mapper.BizArchiveInputMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import cn.ezhizao.project.business.workOrder.mapper.BizWorkOrderMapper;
import cn.ezhizao.project.business.workOrder.domain.BizWorkOrder;
import cn.ezhizao.project.business.workOrder.service.IBizWorkOrderService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 工单信息Service业务层处理
 *
 * @author ruoyi
 * @date 2023-10-16
 */
@Service
public class BizWorkOrderServiceImpl  extends ServiceImpl<BizWorkOrderMapper, BizWorkOrder> implements IBizWorkOrderService
{
    @Resource
    private BizArchiveInputMapper bizArchiveInputMapper;
    @Resource
    private BizWorkOrderMapper bizWorkOrderMapper;
    @Resource
    private BizEntrustMapper bizEntrustMapper;
    final String[] reportTaskTableNames = {"biz_financial_individual_income_tax", "biz_financial_keep_account", "biz_financial_receive_ticket", "biz_financial_report_tax", "biz_financial_salary"};

    final String[] housingFundTaskTableNames = {"biz_housing_fund_confirm", "biz_housing_fund_declare"};

    final String[] socialSecurityTaskTableNames = {"biz_social_security_confirm", "biz_social_security_declare"};

    /**
     * 查询工单信息列表
     *
     * @param bizWorkOrder 工单信息
     * @return 工单信息
     */
    @Override
    public List<BizWorkOrder> getList(BizWorkOrder bizWorkOrder)
    {
        return bizWorkOrderMapper.getList(bizWorkOrder);
    }

    @Override
    public List<BizWorkOrder> getIndexList(BizWorkOrder bizWorkOrder) {
        return bizWorkOrderMapper.getIndexList(bizWorkOrder);
    }

    /**
     * 物理删除
     * @param bizWorkOrder
     * @return 删除结果
     */
    @Override
    public int physicalDelete(BizWorkOrder bizWorkOrder){ return bizWorkOrderMapper.physicalDelete(bizWorkOrder); }

    @Override
    public int insertTasks(List<Map<String, Object>> tableValues) {
        return bizWorkOrderMapper.insertTasks(tableValues);
    }

    @Override
    public int stopTasks(List<Map<String, Object>> tableValues) {
        return bizWorkOrderMapper.stopTasks(tableValues);
    }

    @Override
    public void continueStop(List<BizWorkOrder> list) {

        for(BizWorkOrder bizWorkOrder : list){
//            Date endMonth = bizWorkOrder.getEndMonth();
            //工单实际结束时间，代理记账要多一个月
            Date endDate;
            LocalDate now = LocalDate.now();
            // 获取上一个月的第一天
            LocalDate firstDayOfLastMonth = now.minusMonths(1).withDayOfMonth(1);
            LocalDate firstDayOfMonth = now.withDayOfMonth(1);
            if (bizWorkOrder.getTaskTypeId()==1){
                // 将 LocalDate 对象转换为 Date 对象
                endDate = Date.from(firstDayOfLastMonth.atStartOfDay(ZoneId.systemDefault()).toInstant());
                if(endDate.compareTo(bizWorkOrder.getEndMonth())>0){
                    //endDate = Date.from(firstDayOfMonth.atStartOfDay(ZoneId.systemDefault()).toInstant());
                    bizEntrustMapper.deleteByDate(bizWorkOrder.getId(),endDate,bizWorkOrder.getTaskTypeId(), SecurityUtils.getUserId());
                }
            }else {
                // 将 LocalDate 对象转换为 Date 对象
                endDate = Date.from(firstDayOfMonth.atStartOfDay(ZoneId.systemDefault()).toInstant());
                if(endDate.compareTo(bizWorkOrder.getEndMonth())>0){
                    bizEntrustMapper.deleteByDate(bizWorkOrder.getId(),endDate,bizWorkOrder.getTaskTypeId(), SecurityUtils.getUserId());
                }
            }
        }
    }

    @Override
    public int setContinue(List<Long> ids, Integer state) {

        return bizWorkOrderMapper.setContinue(ids, state);
    }

    @Override
    public void updateByTenantId(Long tenantId) {
        bizWorkOrderMapper.updateByTenantId(tenantId);
    }

    @Override
    public List<BizWorkOrder> getList2(BizWorkOrder bizWorkOrder) {
        return bizWorkOrderMapper.getList2(bizWorkOrder);
    }
    @Override
    public void updateByWorkOrderId(List<Long> ids) {
        bizWorkOrderMapper.updateByWorkOrderId(ids);
    }

    public boolean stopLoopTasks(BizWorkOrder workOrder) {
        if (workOrder.getTaskTypeId() == 1L) {
            return stopReportTask(workOrder);
        } else if (workOrder.getTaskTypeId() == 2L) {
            return stopSocialSecurity(workOrder);
        } else if (workOrder.getTaskTypeId() == 3L) {
            return stopHousingFund(workOrder);
        }
        return true;
    }

    private boolean stopHousingFund(BizWorkOrder workOrder) {
        List<Map<String, Object>> maps = Arrays.stream(housingFundTaskTableNames).map(v -> {
            Map<String, Object> map = new HashMap<>();
            map.put("tableName", v);
            map.put("workOrderId", workOrder.getId());
            map.put("is_continue", 0);
            return map;
        }).collect(Collectors.toList());
        this.stopTasks(maps);
        return true;
    }

    private boolean stopSocialSecurity(BizWorkOrder workOrder) {
        List<Map<String, Object>> maps = Arrays.stream(socialSecurityTaskTableNames).map(v -> {
            Map<String, Object> map = new HashMap<>();
            map.put("tableName", v);
            map.put("workOrderId", workOrder.getId());
            map.put("is_continue", 0);
            return map;
        }).collect(Collectors.toList());
        this.stopTasks(maps);
        return true;
    }

    private boolean stopReportTask(BizWorkOrder workOrder) {
        List<Map<String, Object>> maps = Arrays.stream(reportTaskTableNames).map(v -> {
            Map<String, Object> map = new HashMap<>();
            map.put("tableName", v);
            map.put("workOrderId", workOrder.getId());
            map.put("is_continue", 0);
            return map;
        }).collect(Collectors.toList());
        this.stopTasks(maps);
        return true;
    }
}
