package jnpf.personnels.utils;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import jnpf.ImRobotApi;
import jnpf.base.ActionResult;
import jnpf.from.*;
import jnpf.model.personnels.po.FtbPersonnelsStaffEmploymentApply;
import jnpf.personnels.service.FtbPersonnelsStaffEmploymentApplyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.util.UriUtils;

import java.nio.charset.StandardCharsets;
import java.util.LinkedList;
import java.util.List;

/**
 *
 */
@Component
@Slf4j
public class FtbPersonnlesIMUtils {

    @Autowired
    private ImRobotApi imRobotApi;
    private static final String NOTICE_APP_NAME = "人事管理";
    private static final String APP_LOGO = "https://jnpf-resource-1304460613.cos.ap-chengdu.myqcloud.com/jnpf-resources/UserAvatar/665ecb69e4b0ae5df114c87a.png";
    private static final String BUTTON_NAME = "去填写";
    private static final String BUTTON_NAME_BANLI = "去办理";
    private static final String BUTTON_LINK = "%s/pages/personnel/archives/verify?tenantId=%s&code=%s&phone=%s&name=%s&idCardNum=%s";//跳转页面链接

    private static final String BUTTON_LINK_banli = "pages/personnel/personnelManage/index";//跳转页面链接
    private static final String BUTTON_LINK_Page = "pages/webview/webview";//跳转页面链接

    @Autowired
    FtbPersonnelsStaffEmploymentApplyService staffEmploymentApplyService;

    @Value("${personnel.ementry.url}")
    private String url;

    /**
     * 发送消息
     *
     * @param tenantId 租户
     * @return
     */
    public Boolean sendMsg(String tenantId, String toUserId, String userName) {
        SingleSendRobotNoticeForm form = new SingleSendRobotNoticeForm();
        form.setRobotTypeEnum(ImRobotTypeEnum.FTB_XZS);
        form.setTenantId(tenantId);
        SendRobotNoticeDataForm robotNoticeDataForm = new SendRobotNoticeDataForm();
        robotNoticeDataForm.setLogo(APP_LOGO);//固定图片
        robotNoticeDataForm.setAppName(NOTICE_APP_NAME);
        robotNoticeDataForm.setTitle("邀请填写登记表");
        robotNoticeDataForm.setContent(userName+"邀请你填写登记表");
        JumpUrlListModel jumpUrlListModel = new JumpUrlListModel();
        jumpUrlListModel.setDisplayMethodEnum(JumpUrlListModel.DisplayMethodEnum.TRUE);
        jumpUrlListModel.setButtonName(BUTTON_NAME);
        jumpUrlListModel.setReqMethod(JumpUrlListModel.ReqMethodEnum.GET);
        jumpUrlListModel.setType(1);
        String queryCode = staffEmploymentApplyService.queryCode();
        LambdaQueryWrapper<FtbPersonnelsStaffEmploymentApply> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(FtbPersonnelsStaffEmploymentApply::getUserId,toUserId);
        queryWrapper.eq(FtbPersonnelsStaffEmploymentApply::getEnabledMark,0);
        FtbPersonnelsStaffEmploymentApply serviceOne = staffEmploymentApplyService.getOne(queryWrapper);
        String workerName = serviceOne.getWorkerName();
        String encode = UriUtils.encode(workerName, StandardCharsets.UTF_8);
        String idCardNum = serviceOne.getIdCardNum();
        if (idCardNum == null) {
            idCardNum = "";
        }
        String encodeIdCardNum = UriUtils.encode(idCardNum, StandardCharsets.UTF_8);
        String format = String.format(BUTTON_LINK,url, tenantId, queryCode, serviceOne.getPhone(), encode, encodeIdCardNum);
        jumpUrlListModel.setUrl(format);
        MiniAppUrl miniAppUrl = new MiniAppUrl();
        miniAppUrl.setMpPage(BUTTON_LINK_Page);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("url",format);
        miniAppUrl.setMpParam(jsonObject.toJSONString());
        miniAppUrl.setMpId("__UNI__1EF82DA");
        jumpUrlListModel.setMiniAppUrl(miniAppUrl);
        jumpUrlListModel.setMpId("__UNI__1EF82DA");
        LinkedList<JumpUrlListModel> jumpUrlListModel1 = new LinkedList<>();
        jumpUrlListModel1.add(jumpUrlListModel);
        robotNoticeDataForm.setJumpUrlList(jumpUrlListModel1);
        form.setRobotNoticeDataForm(robotNoticeDataForm);
        form.setToUserIds(List.of(toUserId));
        ActionResult actionResult = imRobotApi.sendSingleRobotNotice(form);
        Integer code = actionResult.getCode();
        if (code.equals(200)) {
            log.info("send msg success");
            return true;
        } else {
            log.error("send msg fail");
            return false;
        }
    }


    /**
     * 发送消息
     *
     * @param tenantId 租户
     * @param moduleId
     * @return
     */
    public Boolean sendMsgWithList(String tenantId, List<String> toUserId, String userName, String moduleId) {
        if(toUserId == null){
            return true;
        }
        SingleSendRobotNoticeForm form = new SingleSendRobotNoticeForm();
        form.setRobotTypeEnum(ImRobotTypeEnum.FTB_XZS);
        form.setTenantId(tenantId);
        SendRobotNoticeDataForm robotNoticeDataForm = new SendRobotNoticeDataForm();
        robotNoticeDataForm.setLogo(APP_LOGO);//固定图片
        robotNoticeDataForm.setAppName(NOTICE_APP_NAME);
        robotNoticeDataForm.setTitle("登记表填写完成");
        robotNoticeDataForm.setContent(userName+"已完成登记表填写，您可以前往入职管理模块为该员工办理入职");
        JumpUrlListModel jumpUrlListModel = new JumpUrlListModel();
        jumpUrlListModel.setDisplayMethodEnum(JumpUrlListModel.DisplayMethodEnum.TRUE);
        jumpUrlListModel.setButtonName(BUTTON_NAME_BANLI);
        jumpUrlListModel.setReqMethod(JumpUrlListModel.ReqMethodEnum.GET);
        jumpUrlListModel.setType(1);
        jumpUrlListModel.setUrl(BUTTON_LINK_banli);
        jumpUrlListModel.setMpId("__UNI__1EF82DA");

        MiniAppUrl miniAppUrl = new MiniAppUrl();
        miniAppUrl.setMpPage(BUTTON_LINK_banli);
        miniAppUrl.setMpId("__UNI__1EF82DA");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("moduleId",moduleId);
        miniAppUrl.setMpParam(jsonObject.toJSONString());
        jumpUrlListModel.setMiniAppUrl(miniAppUrl);

        LinkedList<JumpUrlListModel> jumpUrlListModel1 = new LinkedList<>();
        jumpUrlListModel1.add(jumpUrlListModel);
        robotNoticeDataForm.setJumpUrlList(jumpUrlListModel1);
        form.setRobotNoticeDataForm(robotNoticeDataForm);
        form.setToUserIds(toUserId);
        ActionResult actionResult = imRobotApi.sendSingleRobotNotice(form);
        Integer code = actionResult.getCode();
        if (code.equals(200)) {
            log.info("send msg success");
            return true;
        } else {
            log.error("send msg fail");
            return false;
        }
    }


}
