package com.engine.htxc.cggl.service.impl;

import com.alibaba.fastjson.JSON;
import com.engine.core.impl.Service;
import com.engine.htxc.cggl.service.CgglService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import weaver.conn.RecordSet;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.Util;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.workflow.logging.Logger;
import weaver.workflow.logging.LoggerFactory;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ecology-9
 * @Company: 泛微软件
 * @author: K1810039
 * @version: 1.0
 * @CreateDate: 2022-08-25 16:15
 * @Description:
 * @ModifyLog:
 **/
@Slf4j
public class CgglServiceImpl  extends Service implements CgglService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public Map<String, Object> insertLxxm(Map<String, Object> paramsMap) {
        logger.info("IT立项参数："+ JSON.toJSONString(paramsMap));
        Map<String , Object> map = new HashMap<>();
        RecordSet rs = new RecordSet();
        PropBean pb = new PropBean();
        String modeid = pb.getPropName("lxxm_modeid");
        String xmbh = String.valueOf(paramsMap.get("xmbh"));
        String itxmbh = String.valueOf(paramsMap.get("itxmbh"));
        String xglc = String.valueOf(paramsMap.get("itgllc"));
        String xmcjrq = String.valueOf(paramsMap.get("xmcjrq"));
        String xmmc = String.valueOf(paramsMap.get("xmmc"));
        String ysje = String.valueOf(paramsMap.get("ysje"));
        String xmfzbm = String.valueOf(paramsMap.get("xmfzbm"));
        String xmfzr = String.valueOf(paramsMap.get("xmfzr"));
        String sjghxm = String.valueOf(paramsMap.get("sjghxm"));
        String ly = String.valueOf(paramsMap.get("ly"));
        String zt = String.valueOf(paramsMap.get("zt"));
        String xmlx = String.valueOf(paramsMap.get("xmlx"));
        String lxlcmc = String.valueOf(paramsMap.get("lxlcmc"));

        DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("HH:mm:ss");
        LocalDateTime now2 = LocalDateTime.now();
        String nowDateStr = now2.format(formatter1);
        String nowTimeStr = now2.format(formatter2);
        String insertSql="insert into UF_CGXMGL (lxlcmc,xmbh,itxmbh,xglc,xmcjrq,xmmc,ysje,xmfzbm,xmfzr,sjghxm,ly,zt,xmlx,FORMMODEID,MODEDATACREATER,MODEDATACREATERTYPE,MODEDATACREATEDATE,MODEDATACREATETIME) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        logger.info("insertSql"+insertSql);
        try {
            boolean b = rs.executeUpdate(insertSql,lxlcmc,xmbh,itxmbh,xglc,xmcjrq,xmmc,ysje,xmfzbm,xmfzr,sjghxm,ly,zt,xmlx,modeid,'1','0',nowDateStr,nowTimeStr);
            if (b){
                // 建模权限重构
                ModeRightInfo modeRightInfo = new ModeRightInfo();
                modeRightInfo.setNewRight(true);
                if (rs.execute("select max(id) as maxid from UF_CGXMGL") && rs.next()){
                    int billid = rs.getInt("maxid");
                    modeRightInfo.editModeDataShare(1, Integer.parseInt(modeid),billid);
                }
                map.put("success",true);
            }else {
                map.put("success",false);
            }
        } catch (NumberFormatException e) {
            logger.error("执行插入出错:",e);
        }
        return map;
    }

    @Override
    public Map<String, Object> insertCgxm(Map<String, Object> paramsMap) {
        logger.info("采购立项参数："+ JSON.toJSONString(paramsMap));
        Map<String , Object> map = new HashMap<>();
        RecordSet rs = new RecordSet();
        PropBean pb = new PropBean();
        String modeid = pb.getPropName("cgxm_modeid");

        String cgxmlx = "";
        String itxmbh = String.valueOf(paramsMap.get("cgxmlx"));

        // 通过 itxmbh 项目编号 到立项项目建模中匹配itxmbh与此相等的数据id，赋值给 采购项目立项
        if (StringUtils.isNotBlank(itxmbh)){
            String sql = "select id from UF_CGXMGL where itxmbh like '%"+itxmbh+"%'";
            logger.info(sql);
            rs.execute(sql);
            if (rs.next()){
                cgxmlx = Util.null2String(rs.getString("id"));
            }
        }

        DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("HH:mm:ss");
        LocalDateTime now2 = LocalDateTime.now();
        String nowDateStr = now2.format(formatter1);
        String nowTimeStr = now2.format(formatter2);

        String insertSql="insert into uf_cgxq (cgxmlx,FORMMODEID,MODEDATACREATER,MODEDATACREATERTYPE,MODEDATACREATEDATE,MODEDATACREATETIME) values(?,?,?,?,?,?)";
        logger.info(insertSql);
        boolean b = rs.executeUpdate(insertSql,cgxmlx,modeid,'1','0',nowDateStr,nowTimeStr);
        if (b){
            // 建模权限重构
            ModeRightInfo modeRightInfo = new ModeRightInfo();
            modeRightInfo.setNewRight(true);
            if (rs.execute("select max(id) as maxid from uf_cgxq") && rs.next()){
                int billid = rs.getInt("maxid");
                modeRightInfo.editModeDataShare(1, Integer.parseInt(modeid),billid);
            }
            map.put("success",true);
        }else {
            map.put("success",false);
        }
        return map;
    }

    @Override
    public Map<String, Object> itcgInsertCgxm(Map<String, Object> paramsMap) {
        logger.info("采购立项参数："+ JSON.toJSONString(paramsMap));
        Map<String , Object> map = new HashMap<>();
        RecordSet rs = new RecordSet();
        PropBean pb = new PropBean();
        String modeid = pb.getPropName("cgxm_modeid");

        String cgxmlx = "";
        String xmmc = String.valueOf(paramsMap.get("xmmc"));
        String xmlb = String.valueOf(paramsMap.get("xmlb"));
        String xmlx1 = String.valueOf(paramsMap.get("xmlx1"));
        String xmbh = String.valueOf(paramsMap.get("xmbh"));
        String cgfs = String.valueOf(paramsMap.get("cgfs"));
        String gys = String.valueOf(paramsMap.get("gys"));
        String cgnr = String.valueOf(paramsMap.get("cgnr"));
        String xmfzr = String.valueOf(paramsMap.get("xmfzr"));
        String ysje = String.valueOf(paramsMap.get("ysje"));
        String ysssbm = String.valueOf(paramsMap.get("ysssbm"));
        String cgsl = String.valueOf(paramsMap.get("cgsl"));
        String bz = String.valueOf(paramsMap.get("bz"));
        String fj = String.valueOf(paramsMap.get("fj"));
        String xglc = String.valueOf(paramsMap.get("itgllc"));
        String cgzd1 = String.valueOf(paramsMap.get("cgzd1"));
        String cgtk1 = String.valueOf(paramsMap.get("cgtk1"));
        String ly = String.valueOf(paramsMap.get("ly"));
        String zt = String.valueOf(paramsMap.get("zt"));
        String cjrq = String.valueOf(paramsMap.get("cjrq"));
        String itxmbh = String.valueOf(paramsMap.get("cgxmlx"));

        // 通过 itxmbh 项目编号 到立项项目建模中匹配itxmbh与此相等的数据id，赋值给 采购项目立项
        if (StringUtils.isNotBlank(itxmbh)){
            String sql = "select id from UF_CGXMGL where itxmbh like '%"+itxmbh+"%'";
            logger.info(sql);
            rs.execute(sql);
            if (rs.next()){
                cgxmlx = Util.null2String(rs.getString("id"));
            }
        }

        DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("HH:mm:ss");
        LocalDateTime now2 = LocalDateTime.now();
        String nowDateStr = now2.format(formatter1);
        String nowTimeStr = now2.format(formatter2);

        String insertSql="insert into uf_cgxq (cgxmlx,xmmc,xmlb,xmlx1,xmbh,cgfs,gys,cgnr,xmfzr,ysssbm,cgsl,bz,fj,xglc,cgzd1,cgtk1,ysje,ly,zt,cjrq,FORMMODEID,MODEDATACREATER,MODEDATACREATERTYPE,MODEDATACREATEDATE,MODEDATACREATETIME) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        logger.info(insertSql);
        boolean b = rs.executeUpdate(insertSql,cgxmlx,xmmc,xmlb,xmlx1,xmbh,cgfs,gys,cgnr,xmfzr,ysssbm,cgsl,bz,fj,xglc,cgzd1,cgtk1,ysje,ly,zt,cjrq,modeid,'1','0',nowDateStr,nowTimeStr);
        if (b){
            // 建模权限重构
            ModeRightInfo modeRightInfo = new ModeRightInfo();
            modeRightInfo.setNewRight(true);
            if (rs.execute("select max(id) as maxid from uf_cgxq") && rs.next()){
                int billid = rs.getInt("maxid");
                modeRightInfo.editModeDataShare(1, Integer.parseInt(modeid),billid);
            }
            map.put("success",true);
        }else {
            map.put("success",false);
        }
        return map;
    }

    /**
     * 移动端点击项目编号跳转建模，此方法用于获取跳转URL
     */
    @Override
    public String getModeUrl() {
        log.info("--impl--getModeUrl--");
        PropBean propBean = new PropBean();
        String appHomepageId = propBean.getPropName("cggl_appHomepageId");
        log.info("appHomepageId:[{}]",appHomepageId);
        String url = "/mobilemode/mobile/view.html?appHomepageId="+appHomepageId+"&billid=";
        return url;
    }

}

