package com.weaver.seconddev.njwebservice;

import com.weaver.common.i18n.tool.util.I18nContextUtil;
import com.weaver.seconddev.njwebservice.entity.VendorPunishReq;
import com.weaver.seconddev.njwebservice.entity.VendorPunishReqData;
import com.weaver.seconddev.njwebservice.entity.VendorPunishResponseInfo;
import com.weaver.seconddev.njwebservice.entity.VendorPunishReturnData;
import com.weaver.seconddev.njwebservice.util.SecondUtil;
import com.weaver.verupgrade.conn.CONN_TYPE;
import com.weaver.verupgrade.conn.RecordSet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * @Author liang.cheng
 * @Date 2025/1/20 15:44
 * @Description: 供应商绩效考核处罚流程
 * @Version 1.0
 */
@Service("vendorPunishApplicationWebService")
public class VendorPunishApplicationService {

    private final static Logger log = LoggerFactory.getLogger(VendorPunishApplicationService.class);


    public VendorPunishResponseInfo vendorPunishApplicate(VendorPunishReq param) {

        log.error("VendorPunishApplicationImpl-----start"+param.toString());

        RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
        String poolname = CONN_TYPE.workflow.getType();
        VendorPunishResponseInfo res = new VendorPunishResponseInfo();

        res.setItfId(param.getITFID());
        res.setSendId(param.getSENDID());
        res.setSysId(param.getSYSID());
        
        VendorPunishReturnData resData = new VendorPunishReturnData();
        VendorPunishReqData data = param.getDATAS();
        String workflowId = data.getWorkflowId();
        String requestId = data.getRequestId();
        String type = data.getType();
        String isAgree = data.getIsAgree();
        String supplierOpinion = data.getSupplierOpinion();
        int bearMoney = data.getBearMoney();

        resData.setRequestId(requestId);
        resData.setWorkflowId(workflowId);
        res.setReturnData(resData);
        if (workflowId == null || workflowId.equals("")) {
            resData.setIsTat("E");
            log.error("流程id为空!");
            resData.setMessage("流程id为空！");
            return res;
        }

        String tablename = SecondUtil.getTableName(workflowId);
        log.error("tablename= " + tablename);
        log.error("requestId= " + requestId);
        log.error("type= " + type);
        if (requestId == null || requestId.equals("")) {
            resData.setIsTat("E");
            log.error("请求id为空!");
            resData.setMessage("请求id为空！");
            return res;
        }
        if (type == null || type.equals("")) {
            resData.setIsTat("E");
            resData.setMessage("类型为空！");
            return res;
        }else{
            if("1".equals(type)){//质量问题确认
                log.error("质量问题确认!");
                if (isAgree == null || isAgree.equals("")) {
                    resData.setIsTat("E");
                    log.error("供应商是否同意为空!");
                    resData.setMessage("供应商是否同意为空！");
                    return res;
                }else if(!isAgree.equals("0")&&!isAgree.equals("1")){
                    resData.setIsTat("E");
                    log.error("供应商是否同意传错!");
                    resData.setMessage("供应商是否同意传错！");
                    return res;
                }
                if (supplierOpinion == null || supplierOpinion.equals("")) {
                    resData.setIsTat("E");
                    log.error("供应商厂商意见为空!");
                    resData.setMessage("供应商厂商意见为空！");
                    return res;
                }
                String sql = "update e10_core_business.dbo."+tablename+" set sfty='"+isAgree+"',gyscsyj='"+supplierOpinion+"' where id= '"+requestId+"'";
                log.error("VendorPunishApplicationImpl--sql1"+sql);
                boolean b1 = rs.executeSql(sql, poolname);
                log.error("VendorPunishApplicationImpl--"+requestId+"更新成功:b1="+b1);
            }else if("2".equals(type)){//罚款金额确认
                log.error("罚款金额确认!");
                if (supplierOpinion == null || supplierOpinion.equals("")) {
                    resData.setIsTat("E");
                    log.error("供应商厂商意见为空!");
                    resData.setMessage("供应商厂商意见为空！");
                    return res;
                }
                if(bearMoney<=0){
                    resData.setIsTat("E");
                    log.error("愿意承担金额必须大于02");
                    resData.setMessage("愿意承担金额必须大于0！");
                    return res;
                }
                String sql = "update e10_core_business.dbo."+tablename+" set gyscsfkyj='"+supplierOpinion+"',yycdje='"+bearMoney+"' where id= '"+requestId+"'";
                log.error("VendorPunishApplicationImpl--sql2"+sql);
                boolean b1 = rs.executeSql(sql, poolname);

                log.error("VendorPunishApplicationImpl--"+requestId+"更新成功:b2"+b1);
            }else{

                resData.setIsTat("E");
                resData.setMessage("类型应为1或者2！");
                return res;
            }
        }
        resData.setIsTat("S");
        resData.setMessage("接口调用成功！");
        return res;
    }


}
