package com.kingdee.eas.custom.esign.handler;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.bos.metadata.entity.SelectorItemCollection;
import com.kingdee.bos.metadata.entity.SelectorItemInfo;
import com.kingdee.eas.basedata.person.PersonInfo;
import com.kingdee.eas.custom.esign.ESignGlobalStatusOverviewFactory;
import com.kingdee.eas.custom.esign.ESignGlobalStatusOverviewInfo;
import com.kingdee.eas.custom.esign.IESignGlobalStatusOverview;
import com.kingdee.eas.custom.esign.bizEnum.SendStatusEnum;
import com.kingdee.eas.custom.esign.tsign.hz.comm.EsignHttpResponse;
import com.kingdee.eas.custom.esign.tsign.hz.exception.EsignException;
import com.kingdee.eas.custom.esign.util.EsignHttpUtil;
import com.kingdee.shr.base.syssetting.api.bean.BatchMessageTipsBody;
import com.kingdee.shr.base.syssetting.api.bean.BatchMessageTipsHeader;
import com.kingdee.shr.base.syssetting.context.SHRContext;
import com.kingdee.shr.base.syssetting.exception.SHRWebException;
import com.kingdee.shr.base.syssetting.exception.ShrWebBizException;
import com.kingdee.shr.base.syssetting.web.handler.ListHandler;
import org.springframework.ui.ModelMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * description: ESignGlobalStatusOverviewHandler <br>
 * date: 21/11/2025 下午 4:19 <br>
 * author: lhbj <br>
 * version: 1.0 <br>
 */
public class ESignGlobalStatusOverviewListHandler extends ListHandler {
    public Context getCtx() {
        return SHRContext.getInstance().getContext();
    }
    /**
     * 基于文件发起签署
     * @param request
     * @param response
     * @param modelMap
     * @return
     * @throws SHRWebException
     */
    public String create_by_fileAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws SHRWebException {
        BatchMessageTipsHeader batchMessageTipsHeader = new BatchMessageTipsHeader();
        String billId = this.getBillId(request);
        int failure = 0;
        int success = 0;
        try {
            if(billId.indexOf(",")>=0){
                throw new EsignException("请选择一行记录发起签署");
            }
            IESignGlobalStatusOverview globalStatusOverview = ESignGlobalStatusOverviewFactory.getLocalInstance(this.getCtx());
            SelectorItemCollection selectorItemCollection = new SelectorItemCollection();
            selectorItemCollection.add(new SelectorItemInfo("*"));
            selectorItemCollection.add(new SelectorItemInfo("person.id"));
            selectorItemCollection.add(new SelectorItemInfo("operator.id"));
            ESignGlobalStatusOverviewInfo info = globalStatusOverview.getESignGlobalStatusOverviewInfo(new ObjectUuidPK(billId));
            PersonInfo person = info.getPerson();
            PersonInfo operator = info.getOperator();
            String personId=null;
            if(null!=person&&null!=person.getId()){
                personId=person.getId().toString();
            }
            String operatorId=null;
            if(null!=operator&&null!=operator.getId()){
                operatorId=operator.getId().toString();
            }
            EsignHttpResponse httpRes =EsignHttpUtil.create_by_file(this.getCtx(),personId,info.getFileName(),operatorId,info.getEfileId(),
                    info.getEsignStatus(),info.getRequestParams(),false);
            if(httpRes.getStatus()>=200&&httpRes.getStatus()<300) {
                JSONObject object = JSON.parseObject(httpRes.getBody());
                if("0".equals(String.valueOf(object.get("code")))) {

                    BatchMessageTipsBody body = new BatchMessageTipsBody();
                    body.setMuitTipsState(Boolean.TRUE);
                    body.setMuitTipsMessage(httpRes.getBody());
                    body.setId(billId);
                    batchMessageTipsHeader.addResult(body);
                    success++;
                }else {
                    BatchMessageTipsBody body = new BatchMessageTipsBody();
                    body.setMuitTipsState(Boolean.FALSE);
                    body.setMuitTipsMessage(httpRes.getBody());
                    body.setId(billId);
                    batchMessageTipsHeader.addResult(body);
                }
            }else {
                BatchMessageTipsBody body = new BatchMessageTipsBody();
                body.setMuitTipsState(Boolean.FALSE);
                body.setMuitTipsMessage(httpRes.getBody());
                body.setId(billId);
                batchMessageTipsHeader.addResult(body);
            }
            batchMessageTipsHeader.setBillId(billId);
            batchMessageTipsHeader.setFailureCount((success>0?1:0));
            batchMessageTipsHeader.setSuccessCount(success);
            this.writeSuccessData(batchMessageTipsHeader);
        }catch (EsignException e){
            e.printStackTrace();
            throw new ShrWebBizException(e);
        }catch (Exception e){
            e.printStackTrace();
            throw new ShrWebBizException(e);
        }
        return null;
    }
    /**
     * 撤销签署流程
     * @param request
     * @param response
     * @param modelMap
     * @return
     * @throws SHRWebException
     */
    public String revoke_by_fileAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws SHRWebException {
        BatchMessageTipsHeader batchMessageTipsHeader = new BatchMessageTipsHeader();
        String billId = this.getBillId(request);
        String revokeReason =request.getParameter("revokeReason");
        int failure = 0;
        int success = 0;
        try {
            if(billId.indexOf(",")>=0){
                throw new EsignException("请选择一行记录发起签署");
            }
            IESignGlobalStatusOverview globalStatusOverview = ESignGlobalStatusOverviewFactory.getLocalInstance(this.getCtx());
            SelectorItemCollection selectorItemCollection = new SelectorItemCollection();
            selectorItemCollection.add(new SelectorItemInfo("*"));
            selectorItemCollection.add(new SelectorItemInfo("person.id"));
            selectorItemCollection.add(new SelectorItemInfo("operator.id"));
            ESignGlobalStatusOverviewInfo info = globalStatusOverview.getESignGlobalStatusOverviewInfo(new ObjectUuidPK(billId));
            PersonInfo person = info.getPerson();
            PersonInfo operator = info.getOperator();
            String personId=null;
            if(null!=person&&null!=person.getId()){
                personId=person.getId().toString();
            }
            String operatorId=null;
            if(null!=operator&&null!=operator.getId()){
                operatorId=operator.getId().toString();
            }
            EsignHttpResponse httpRes =EsignHttpUtil.revoke_by_file(this.getCtx(),info.getSignFlowId(),revokeReason,info.getEfileId());
            if(httpRes.getStatus()>=200&&httpRes.getStatus()<300) {
                JSONObject object = JSON.parseObject(httpRes.getBody());
                if("0".equals(String.valueOf(object.get("code")))) {

                    BatchMessageTipsBody body = new BatchMessageTipsBody();
                    body.setMuitTipsState(Boolean.TRUE);
                    body.setMuitTipsMessage(httpRes.getBody());
                    body.setId(billId);
                    batchMessageTipsHeader.addResult(body);
                    success++;
                }else {
                    BatchMessageTipsBody body = new BatchMessageTipsBody();
                    body.setMuitTipsState(Boolean.FALSE);
                    body.setMuitTipsMessage(httpRes.getBody());
                    body.setId(billId);
                    batchMessageTipsHeader.addResult(body);
                }
            }else {
                BatchMessageTipsBody body = new BatchMessageTipsBody();
                body.setMuitTipsState(Boolean.FALSE);
                body.setMuitTipsMessage(httpRes.getBody());
                body.setId(billId);
                batchMessageTipsHeader.addResult(body);
            }
            batchMessageTipsHeader.setBillId(billId);
            batchMessageTipsHeader.setFailureCount((success>0?1:0));
            batchMessageTipsHeader.setSuccessCount(success);
            this.writeSuccessData(batchMessageTipsHeader);
        }catch (EsignException e){
            e.printStackTrace();
            throw new ShrWebBizException(e);
        }catch (Exception e){
            e.printStackTrace();
            throw new ShrWebBizException(e);
        }
        return null;
    }
    /**
     * 催签流程中签署人
     * @param request
     * @param response
     * @param modelMap
     * @return
     * @throws SHRWebException
     */
    public String urge_by_fileAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws SHRWebException {
        BatchMessageTipsHeader batchMessageTipsHeader = new BatchMessageTipsHeader();
        String billId = this.getBillId(request);
        int failure = 0;
        int success = 0;
        try {
            if(billId.indexOf(",")>=0){
                throw new EsignException("请选择一行记录发起签署");
            }
            IESignGlobalStatusOverview globalStatusOverview = ESignGlobalStatusOverviewFactory.getLocalInstance(this.getCtx());
            SelectorItemCollection selectorItemCollection = new SelectorItemCollection();
            selectorItemCollection.add(new SelectorItemInfo("*"));
            selectorItemCollection.add(new SelectorItemInfo("person.id"));
            selectorItemCollection.add(new SelectorItemInfo("person.id"));
            ESignGlobalStatusOverviewInfo info = globalStatusOverview.getESignGlobalStatusOverviewInfo(new ObjectUuidPK(billId));
            PersonInfo person = info.getPerson();
            PersonInfo operator = info.getOperator();
            String personId=null;
            if(null!=person&&null!=person.getId()){
                personId=person.getId().toString();
            }
            String operatorId=null;
            if(null!=operator&&null!=operator.getId()){
                operatorId=operator.getId().toString();
            }
            EsignHttpResponse httpRes =EsignHttpUtil.urge_by_file(this.getCtx(),info.getSignFlowId(),null,info.getEfileId());
            if(httpRes.getStatus()>=200&&httpRes.getStatus()<300) {
                JSONObject object = JSON.parseObject(httpRes.getBody());
                if("0".equals(String.valueOf(object.get("code")))) {

                    BatchMessageTipsBody body = new BatchMessageTipsBody();
                    body.setMuitTipsState(Boolean.TRUE);
                    body.setMuitTipsMessage(httpRes.getBody());
                    body.setId(billId);
                    batchMessageTipsHeader.addResult(body);
                    success++;
                }else {
                    BatchMessageTipsBody body = new BatchMessageTipsBody();
                    body.setMuitTipsState(Boolean.FALSE);
                    body.setMuitTipsMessage(httpRes.getBody());
                    body.setId(billId);
                    batchMessageTipsHeader.addResult(body);
                }
            }else {
                BatchMessageTipsBody body = new BatchMessageTipsBody();
                body.setMuitTipsState(Boolean.FALSE);
                body.setMuitTipsMessage(httpRes.getBody());
                body.setId(billId);
                batchMessageTipsHeader.addResult(body);
            }
            batchMessageTipsHeader.setBillId(billId);
            batchMessageTipsHeader.setFailureCount((success>0?1:0));
            batchMessageTipsHeader.setSuccessCount(success);
            this.writeSuccessData(batchMessageTipsHeader);
        }catch (EsignException e){
            e.printStackTrace();
            throw new ShrWebBizException(e);
        }catch (Exception e){
            e.printStackTrace();
            throw new ShrWebBizException(e);
        }
        return null;
    }

}
