package nckd.jimin.jyyy.fi.common;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.fi.fa.business.constants.BdUser;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 财务帮助类
 * @author wanghaiwu_kd
 * @date 2025/04/29
 */
public class FinanceCommonUtils {

    private static final Log logger = LogFactory.getLog(FinanceCommonUtils.class);

    /**
     * 校验出差申请单、外办单是否存在与参数startDate, endDate存在时间交叉的记录
     * @param startDate
     * @param endDate
     * @return
     */
    public static String checkDateIsExists(Long billId, Long userId, Date startDate, Date endDate){
        String checkMsg = "";

        String selectFields = "id, billno";
        String entityName = "";
        //出差申请单
        entityName = "er_tripreqbill";
        QFilter qFilter = buildSearchFilter(entityName, billId, userId, startDate, endDate);
        DynamicObject[] tripReqBillArray = BusinessDataServiceHelper.load(entityName, selectFields, qFilter.toArray());

        //外办单
        entityName = "er_dailyvehiclebill";
        qFilter = buildSearchFilter(entityName, billId, userId, startDate, endDate);
        DynamicObject[] vehicleBillArray = BusinessDataServiceHelper.load(entityName, selectFields, qFilter.toArray());

        //HR休假申请
        entityName = "wtabm_vaapplyself";
        qFilter = buildSearchFilter(entityName, billId, userId, startDate, endDate);
        DynamicObject[] vaapplyBillArray = BusinessDataServiceHelper.load(entityName, selectFields, qFilter.toArray());

        String tripReqBills = "";
        if(tripReqBillArray.length > 0){
            List<String> numberList = (List) Arrays.stream(tripReqBillArray).map((o) -> {
                return o.getString("billno");
            }).collect(Collectors.toList());

            tripReqBills = String.join(",", numberList);
        }

        String vehicleBills = "";
        if(vehicleBillArray.length > 0){
            List<String> numberList = (List) Arrays.stream(vehicleBillArray).map((o) -> {
                return o.getString("billno");
            }).collect(Collectors.toList());

            vehicleBills = String.join(",", numberList);
        }

        String vaapplyBills = "";
        if(vaapplyBillArray.length > 0){
            List<String> numberList = (List) Arrays.stream(vaapplyBillArray).map((o) -> {
                return o.getString("billno");
            }).collect(Collectors.toList());

            vaapplyBills = String.join(",", numberList);
        }


        if(tripReqBills.length() > 0){
            checkMsg += "出差申请单：" + tripReqBills;
        }
        if(vehicleBills.length() > 0){
            checkMsg += "外办单：" + vehicleBills;
        }
        if(vaapplyBills.length() > 0){
            checkMsg += "HR休假单：" + vaapplyBills;
        }

        return checkMsg;
    }

    /**
     * 构造查询条件
     * @param entityName
     * @param startDate
     * @param endDate
     * @return
     */
    public static QFilter buildSearchFilter(String entityName, Long billId, Long userId, Date startDate, Date endDate){
        String prefix = "";
        String startField = "startDate";
        String endField = "endDate";

        QFilter qFilter = new QFilter("id", QCP.not_equals, billId);
        if("er_tripreqbill".equals(entityName)){//出差申请
            qFilter.and(new QFilter("applier", QCP.equals, userId));
            //废弃 状态不需要校验
            qFilter.and(new QFilter("billstatus", QCP.not_equals2, "H"));

            prefix = "tripentry.";
        } else if("er_dailyvehiclebill".equals(entityName)){//用车申请
            qFilter.and(new QFilter("applier", QCP.equals, userId));
        } else if("wtabm_vaapplyself".equals(entityName)){//HR休假
            DynamicObject userInfo = BusinessDataServiceHelper.loadSingleFromCache(userId, "bos_user");
            String personNo = userInfo.getString("number");

            //过滤条件：考勤人员.工号
            qFilter.and(new QFilter("personid.number", QCP.equals, personNo));
            //审批不通过、废弃状态不需要校验
            qFilter.and(new QFilter("billstatus", QCP.not_in, new String[]{"E","F"}));
            //哺乳假不参与校验
            qFilter.and(new QFilter("entryentity.entryvacationtype.number", QCP.not_equals2, "1090_S"));
            //申请时长大于0
            qFilter.and(new QFilter("entryentity.entryvatimehour", QCP.large_than, BigDecimal.ZERO));

            prefix = "entryentity.";
            startField = "entrystartdate";
            endField = "entryenddate";
        }

        QFilter startFilter = new QFilter(prefix + startField, QCP.less_equals, startDate);
        startFilter.and(new QFilter(prefix + endField, QCP.large_equals, startDate));

        QFilter endFilter = new QFilter(prefix + startField, QCP.less_equals, endDate);
        endFilter.and(new QFilter(prefix + endField, QCP.large_equals, endDate));

        QFilter startFilter1 = new QFilter(prefix + startField, QCP.large_equals, startDate);
        startFilter1.and(new QFilter(prefix + startField, QCP.less_than, endDate));

        QFilter endFilter1 = new QFilter(prefix + endField, QCP.large_equals, startDate);
        endFilter1.and(new QFilter(prefix + endField, QCP.less_than, endDate));

        qFilter.and(startFilter.or(endFilter).or(startFilter1).or(endFilter1));

        return qFilter;
    }
}
