package com.engine.htxc.contract.dao;

import com.engine.htxc.commonutil.db.EntityCrudUtil;
import com.engine.htxc.contract.dto.SecretLevelEnum;
import com.engine.htxc.contract.entity.CghtMainEntity;
import com.engine.htxc.contract.entity.UfHtqcDt1;
import com.engine.htxc.contract.entity.UfHtqcDt3;
import com.engine.htxc.contract.entity.UfHtqcDt4;
import com.engine.htxc.contract.number.entity.Uf_ContractNoAggr;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetTrans;
import weaver.formmode.setup.ModeRightInfo;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @Title: ecology-9
 * @Company: 泛微软件
 * @author: K1810050
 * @version: 1.0
 * @CreateDate: 2022/8/18 21:00
 * @Description: 采购发起合同dao
 * @ModifyLog: 添加注释
 **/
public class CghtDao {
    private static final Logger logger= LoggerFactory.getLogger(CghtDao.class);

    /**
     * 采购出发合同 用印状态默认为0
     */
    public static final int YYZT_0 = 0;
    /**
     * 采购出发合同 税率默认为0
     */
    public static final String SL_0 = "0";
    public static final String BHSJE_DEFAULT = "0.00";
    public static final String UF_HTQC = "uf_htqc";

    public static CghtMainEntity insertCghtqcMain(Uf_ContractNoAggr contractNo, CghtMainEntity cghtMainEntity){
        //构造默认值
        EntityCrudUtil.buildModeBaseAttributes(UF_HTQC,cghtMainEntity,CghtMainEntity.class);
        buildDefaultCghtValue(cghtMainEntity);
        final RecordSetTrans rst = new RecordSetTrans();
        rst.setAutoCommit(false);
        //插入数据
        try {
            EntityCrudUtil.transInsert(rst,UF_HTQC,cghtMainEntity,CghtMainEntity.class);
            final int mainId = EntityCrudUtil.getModeDataIdByUuid(rst,UF_HTQC, cghtMainEntity.getModeuuid());
            logger.info("----合同起草主表[uf_htqc] id:[{}] ",mainId);
            if (mainId<0) {
                throw new RuntimeException("插入主表[uf_htqc]数据失败");
            }
            cghtMainEntity.setId(mainId);

            logger.info("合同信息保存成功，保存合同编号");
            contractNo.setMbid(String.valueOf(cghtMainEntity.getId()));
            EntityCrudUtil.transInsert(rst,"uf_contractnoaggr",contractNo,Uf_ContractNoAggr.class);

            //插入明细表数据
            insertDetails(rst, mainId);

            //权限重构
            logger.info("权限重构");
            ModeRightInfo ModeRightInfo = new ModeRightInfo();
            ModeRightInfo.setNewRight(true);
            ModeRightInfo.editModeDataShare(cghtMainEntity.getQcr(), cghtMainEntity.getFormmodeid(), mainId);

            rst.commit();
            logger.info("插入数据成功，数据提交");
            return cghtMainEntity;
        } catch (Exception e) {
            logger.error("插入数据失败，数据回滚",e);
            rst.rollback();
        }finally {
            rst.setAutoCommit(true);
        }
        return null;
    }

    private static void insertDetails(RecordSetTrans rst, int mainId) throws Exception {
        //明细表1
        logger.info("插入明细表数据");
        final UfHtqcDt1 ufHtqcDt1 = new UfHtqcDt1();
        ufHtqcDt1.setYyzt(YYZT_0);
        ufHtqcDt1.setMainid(mainId);
        if(!EntityCrudUtil.transInsert(rst,"uf_htqc_dt1", ufHtqcDt1,UfHtqcDt1.class)){
            throw new RuntimeException("插入明细表[uf_htqc_dt1]数据失败");
        }
        //明细表3
        final UfHtqcDt3 ufHtqcDt3 = new UfHtqcDt3();
        ufHtqcDt3.setYyzt(YYZT_0);
        ufHtqcDt3.setMainid(mainId);
        if(!EntityCrudUtil.transInsert(rst,"uf_htqc_dt3",ufHtqcDt3,UfHtqcDt3.class)){
            throw new RuntimeException("插入明细表[uf_htqc_dt3]]数据失败");
        }
        ////明细表4
        final UfHtqcDt4 ufHtqcDt4 = new UfHtqcDt4();
        ufHtqcDt4.setSl(SL_0);
        ufHtqcDt4.setMainid(mainId);
        ufHtqcDt4.setBhsje(BHSJE_DEFAULT);
        if(!EntityCrudUtil.transInsert(rst,"uf_htqc_dt4",ufHtqcDt4,UfHtqcDt4.class)){
            throw new RuntimeException("插入明细表[uf_htqc_dt4]数据失败");
        }
    }



    private static CghtMainEntity buildDefaultCghtValue(CghtMainEntity cghtMainEntity){
        LocalDateTime now = LocalDateTime.now();
        cghtMainEntity.setQcrq(now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        cghtMainEntity.setSfwdsf("0");
        cghtMainEntity.setSfxjwfk(1);
        cghtMainEntity.setDqbb(1);
        cghtMainEntity.setHtjexz(0);
        cghtMainEntity.setSfsw(1);
        cghtMainEntity.setBz(0);
        cghtMainEntity.setZt(0);
        cghtMainEntity.setSfsymbfb(0);
        cghtMainEntity.setBmdj(SecretLevelEnum.NORMAL_BUSINESS_SECRET.ordinal());
        cghtMainEntity.setSflx(1);
        cghtMainEntity.setHtxz(0);
        cghtMainEntity.setModedatacreater(cghtMainEntity.getQcr());
        cghtMainEntity.setFhtlx(getParentCategory(cghtMainEntity.getHtlx()));
        return cghtMainEntity;
    }

    public static String getParentCategory(String category){
        final RecordSet rs=new RecordSet();
        rs.executeQuery("select s.SJFL from uf_type_of_contract s where s.BZSM=?",category);
        if(rs.next()){
            return rs.getString(1);
        }
        return "";
    }
}
