package com.engine.attendance.workflow.action;

import com.engine.common.util.DbTools;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.*;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.*;

/**
 * @Author: sy
 * @Description: 撤销出差
 * @Date: 2024/3/15
 **/
@Slf4j
public class RevokeLeaveAction implements Action {

    @Override
    public String execute(RequestInfo requestInfo) {
        log.info("RevokeLeaveAction,do action on request:" + requestInfo.getRequestid());
        String requestid = requestInfo.getRequestid();
        try {

//            Property[] property = requestInfo.getMainTableInfo().getProperty();
//            User user = requestInfo.getRequestManager().getUser();
//            for (Property pro : property) {
//                String name = Util.null2String(pro.getName());
//                if(name.equals("cxsm")){
//
//                }
//            }

            DetailTable[] detailTable = requestInfo.getDetailTableInfo().getDetailTable();
            log.info("RevokeLeaveAction detailTable : [{}], length : {}", Arrays.toString(detailTable), detailTable.length);
            List<Map<String, String>> updateList = new ArrayList<>();
            if (detailTable.length > 0) {
                for (DetailTable dt : detailTable) {
                    //明细表
                    Row[] s = dt.getRow();
                    for (Row r : s) {
                        //行
                        Cell[] h = r.getCell();
                        Map<String, String> itemMap = new HashMap<>();
                        for (Cell c : h) {
                            //列
                            String name = Util.null2String(c.getName());
                            log.info("RevokeLeaveAction key : {}, value : {}",c.getName(), c.getValue());
                            if ("cxsm".equals(name)) {
                                itemMap.put("cxsm", Util.null2String(c.getValue()));
                            }
                            if ("cxlyid".equals(name)) {
                                itemMap.put("cxlyid", Util.null2String(c.getValue()));
                            }
                            if ("cx".equals(name)) {
                                itemMap.put("cx", Util.null2String(c.getValue()));
                            }
                        }
                        log.info("RevokeLeaveAction itemMap : [{}]",itemMap);
                        if (Util.null2String(itemMap.get("cx")).equals("1")) {
                            updateList.add(itemMap);
                        }
                    }
                }
            }
            log.info("RevokeLeaveAction updateList : [{}]",updateList);
            if (updateList.size() > 0) {
                updateList.forEach(f -> {
                    if (StringUtils.isNotBlank(f.get("cxlyid"))) {
                        String sql = "update uf_jcl_kq_ccjl_dt1" +" set "+"cxly=0,cxcc=1,cxsm=?,cxid=? where id = ?";
                        log.info("RevokeLeaveAction sql : [{}]",sql);
                        DbTools.update(sql, Util.null2String(f.get("cxsm")), requestid, f.get("cxlyid"));
                        log.info("RevokeLeaveAction cxsm : {}, requestid : {}, cxlyid : {}",Util.null2String(f.get("cxsm")), requestid, f.get("cxlyid"));
                    }
                });
            }

        } catch (Exception e) {
            log.info("流程数据报错:RevokeLeaveAction:");
            StringWriter errorsWriter = new StringWriter();
            e.printStackTrace(new PrintWriter(errorsWriter));
            log.info(errorsWriter.toString());
            requestInfo.getRequestManager().setMessageid("11111" + requestInfo.getRequestid() + "22222");
            requestInfo.getRequestManager().setMessagecontent("【更改出差撤销状态action】报错，请联系管理员！");
            return Action.FAILURE_AND_CONTINUE;
        }

        return SUCCESS;


    }
}
