package weaver.interfaces.mfkj.action;

import weaver.conn.RecordSet;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.BaseBean;
import weaver.general.TimeUtil;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.UUID;

/**
 * @Title 积分申请流程写入积分台账
 * @Author wangchaofa
 * @CreateDate 2024/2/4
 * @Version 1.0
 * @Description
 */
public class PointsAddAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        try {
            String tablename = requestInfo.getRequestManager().getBillTableName();
            String detailtable = tablename + "_dt1";

            String requestid = requestInfo.getRequestid();
            int creater = requestInfo.getRequestManager().getCreater();

            RecordSet rs = new RecordSet();
            RecordSet rs1 = new RecordSet();
            RecordSet rst = new RecordSet();
            String dataSql = " select b.jfhdr,b.bm,b.gw,sum(b.hdjfs)as count from " + tablename + " a,"+ detailtable +" b where a.id=b.mainid and a.requestid=" + requestid +" group by b.jfhdr,b.bm,b.gw";
            rs.execute(dataSql);
            while (rs.next()) {
                String jfhdr = Util.null2String(rs.getString("jfhdr")); //积分获得人
                String bm = Util.null2String(rs.getString("bm")); //部门
                String gw = Util.null2String(rs.getString("gw")); //岗位
                double hdjfs = Util.getDoubleValue(rs.getString("count"),0); //获得积分数

                String currentdate = TimeUtil.getCurrentDateString();
                String currenttime = TimeUtil.getOnlyCurrentTimeString();
                int jftz_formmodeid = Integer.parseInt(getPropValue("syn_points_mfkj", "jftz_formmodeid"));

                rs1.executeQuery("select * from uf_jftz where jfhdr=?",jfhdr);
                if(rs1.next()){
                    double zjf = Util.getDoubleValue(rs1.getString("zjf"),0);
                    zjf += hdjfs;
                    boolean bool = rst.executeUpdate("update uf_jftz set zjf=? where jfhdr=?",zjf,jfhdr);
                    writeLog(requestid + " ================ 积分申请流程写入积分台账 bool  =============== "+ bool);
                }else{
                    String uuid = UUID.randomUUID().toString();
                    String insertSql = " insert into uf_jftz(jfhdr,jfhdrbm,jfhdrgw,zjf," +
                            "modedatacreater,modedatacreatedate,modedatacreatetime,formmodeid,modeuuid)  " +
                            "values(?,?,?,?,?,?,?,?,?) ";
                    boolean exeRes = rst.executeUpdate(insertSql,new Object[]{jfhdr,bm,gw,hdjfs,
                            creater, currentdate, currenttime, jftz_formmodeid, uuid});
                    int billid = -1;
                    if (exeRes) {
                        //重构权限
                        rs1.executeQuery(" select id from uf_jftz where modeuuid=? ",new Object[]{uuid});
                        rs1.next();
                        billid = rs1.getInt("id");
                        ModeRightInfo moderight = new ModeRightInfo();
                        moderight.editModeDataShare(creater, jftz_formmodeid, billid);
                    }
                }
            }
        } catch (Exception e) {
            requestInfo.getRequestManager().setMessagecontent("积分写入台账失败，请联系管理员！");
            return Action.FAILURE_AND_CONTINUE;
        }
        return Action.SUCCESS;
    }
}
