package com.sys.bus.jx.service;

import com.jfinal.kit.PropKit;
import com.sys.bus.hr.model.Person;
import com.sys.bus.hr.service.HrService;
import com.sys.bus.model.*;
import com.sys.bus.sys.wx.WxMsgType;
import com.sys.bus.sys.wx.WxUtil;
import com.sys.bus.syscode.service.SysCodeService;
import com.sys.core.utils.Tool;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.List;

public class WxMsgService {
    Log log = LogFactory.getLog(this.getClass());
    static HrService hrService = new HrService();
    static SysCodeService codeService = new SysCodeService();
    static String shrUrl = null;
    //static String urgeMsg = null;


//    public void sendEntryWxMsg(String entryId){
//        Entry entry = Entry.dao.findById(entryId);
//        this.sendEntryWxMsg(entry);
//    }
//    //TODO
//    public void sendEntryWxMsg(String entryId,String type,boolean urge){
//        Entry entry = Entry.dao.findById(entryId);
//        this.sendEntryWxMsg(entry,type,urge);
//    }
//    public void sendEntryWxMsg(Entry entry,String type,boolean urge){
//        Person person = null;
//        WxMsgType wxMsgType = null;
//
//        this.sendEntryWxMsg(entry,wxMsgType,person,urge);
//    }
public void sendEntryWxMsg(Entry entry, WxMsgType type, Person person){
    this.sendEntryWxMsg(entry,type,person.getId(),person.getName(),false);
}
//    public void sendEntryWxMsg(Entry entry, WxMsgType type, Person person,boolean urge){
//        this.sendEntryWxMsg(entry,type,person.getId(),person.getName(),urge);
//    }

    public void sendEntryWxMsg(Entry entry, WxMsgType type, String pid, String pname){
        this.sendEntryWxMsg(entry,type,pid,pname,false);
    }
    public void sendEntryWxMsg(Entry entry,WxMsgType type,String pid,String pname,boolean urge){
        String msg = codeService.getSysCodeName(type.getValue()).getSyscodeValue();

        if(StringUtils.isBlank(pid)){
            log.error("pid为空，本次发消息失败！entryId:"+entry.getEntryId());
            return;
        }
        if(StringUtils.isBlank(msg)){
            log.error("未找到配置："+type.getValue()+"，本次发消息失败！");
            return;
        }
        if(urge){
            String urgeMsg = codeService.getSysCodeName("催办文本").getSyscodeValue();
            if(StringUtils.isNotBlank(urgeMsg))msg = urgeMsg+msg;
        }
        String currTime = Tool.getCurrTime();
        String uuid = Tool.uuid();
        msg = msg.replace("{被考核人}",entry.getEntryKhpersonname()).replace("{考核周期}",entry.getEntryCyclename()).replace("{考核组织}",entry.getEntryOrgname());
        WxMsg wxMsg = new WxMsg();
        wxMsg.setWxmsgId(uuid);
        wxMsg.setWxmsgSourceid(entry.getEntryId());
        wxMsg.setWxmsgCreatetime(currTime);
        wxMsg.setWxmsgRetry(0);
        wxMsg.setWxmsgText(msg);
        wxMsg.setWxmsgTopid(pid);
        wxMsg.setWxmsgSendtime(currTime);
        wxMsg.setWxmsgToperson(pname);
        wxMsg.save();

        LarkMsg larkMsg = new LarkMsg();
        larkMsg.setLarkmsgId(uuid);
        larkMsg.setLarkmsgSourceid(entry.getEntryId());
        larkMsg.setLarkmsgCreatetime(currTime);
        larkMsg.setLarkmsgRetry(0);
        larkMsg.setLarkmsgText(msg);
        larkMsg.setLarkmsgTopid(pid);
        larkMsg.setLarkmsgSendtime(currTime);
        larkMsg.setLarkmsgToperson(pname);
        larkMsg.save();

    }
//    public void sendEntryWxMsg(Entry entry,boolean urge,boolean back){
//        if(entry == null){
//            return;
//        }
//        String type = "";
//        String pid = entry.getEntryKhpersonid();
//        String appType = entry.getEntryApptype();//5 编辑KPI 6 上级领导审核KPI    1自评 2评分
//        if("5".equals(appType)){
//            type = "1";
//        }else  if("6".equals(appType)){
//            type = "2";
//            pid = entry.getEntrySuperId();
//        }else  if("1".equals(appType)){
//            type = "3";
//        }else  if("2".equals(appType)){
//            type = "4";
//            pid = entry.getEntrySuperId();
//        }
//        if(pid == null){
//            return;
//        }
//        String typeName = "";
//        boolean checkSameMsg =false;
//        if("1".equals(type)){
//            typeName = "个人提交目标";
//            if(back){
//                typeName = "个人打回目标";
//            }
//        }else if("2".equals(type)){
//            typeName = "领导审批目标";
//            checkSameMsg = true;
//        }else if("3".equals(type)){
//            typeName = "个人自评";
//            if(back){
//                typeName = "个人打回自评";
//            }
//        }else if("4".equals(type)){
//            typeName = "领导评分";
//            checkSameMsg = true;
//        }else{
//            return;
//        }
//        String msg = codeService.getSysCodeName(typeName).getSyscodeValue();
//
//        if(StringUtils.isBlank(msg)){
//            log.error("未找到配置："+typeName+"，本次发消息失败！");
//            return;
//        }
////        String status = "1";
//        Person person = hrService.getPersonById(pid);
//        String currTime = Tool.getCurrTime();
//
//        msg = msg.replace("{被考核人}",entry.getEntryKhpersonname());
//        if(urge){
//            String urgeMsg = codeService.getSysCodeName("催办文本").getSyscodeValue();
//            if(StringUtils.isNotBlank(urgeMsg))msg = urgeMsg+msg;
//        }
//        WxMsg wxMsg = new WxMsg();
//        wxMsg.setWxmsgId(Tool.uuid());
//        wxMsg.setWxmsgSourceid(entry.getEntryId());
//        wxMsg.setWxmsgCreatetime(currTime);
//        wxMsg.setWxmsgRetry(0);
//        wxMsg.setWxmsgText(msg);
//        wxMsg.setWxmsgTopid(pid);
//        wxMsg.setWxmsgSendtime(currTime);
//        wxMsg.setWxmsgToperson(person.getName());
////        String wxUserId = getWxUserId(pid);
////        wxMsg.setWxmsgTo(wxUserId);
////        if(devDebug && StringUtils.isNotBlank(wxMsgTo)){
////            wxUserId = wxMsgTo;
////        }
////        if(StringUtils.isNotBlank(wxUserId)){
////            boolean success = this.sendMsg(wxUserId, msg,checkSameMsg);
////            if(!success){
////                status = "2";
////            }
////        }else{
////            status = "2";
////        }
//
////        wxMsg.setWxmsgStatus(status);
//        wxMsg.save();
//    }
//    public void sendEntryWxMsg(Entry entry){
//        this.sendEntryWxMsg(entry,false,false);
//    }
//    public void sendEntryWxMsgBack(Entry entry,boolean back){
//        this.sendEntryWxMsg(entry,false,back);
//    }


}