package com.zjrs.ggfw.rlzysc.blo.impl;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.mohrss.leaf.core.framework.domain.blo.impl.BLOImpl;
import org.mohrss.leaf.core.framework.persistens.PageResult;
import org.mohrss.leaf.core.framework.persistens.PageUtil;
import org.mohrss.leaf.core.framework.util.CurrentUser;
import org.mohrss.leaf.core.framework.web.exception.AppException;
import com.zjrs.ggfw.rlzysc.blo.MsGlBLO;
import com.zjrs.ggfw.rlzysc.dto.grzxjlxx.ZjDTO;
import com.zjrs.ggfw.rlzysc.dto.msgl.*;
import com.zjrs.ggfw.rlzysc.entity.*;
import com.zjrs.ggfw.rlzysc.po.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

@Service
public class MsGlBLOImpl extends BLOImpl implements MsGlBLO {

    private static final Logger logger = LoggerFactory.getLogger(MsGlBLOImpl.class);

    @Autowired
    private VRsRlzyMsryglMapper vRsRlzyMsryglMapper;

    @Autowired
    private VRsRlzyMsglMapper vRsRlzyMsglMapper;

    @Autowired
    private VRsRlzyMsgglMapper vRsRlzyMsgglMapper;

    @Autowired
    private QyxxMapper qyxxMapper;

    @Autowired
    private ZwxxMapper zwxxMapper;

    @Autowired
    private GwxxMapper gwxxMapper;

    @Override
    public void saveMsglInfo(MsUDTO dto) {

        int cnt = vRsRlzyMsglMapper.selectCount(new QueryWrapper<VRsRlzyMsglPO>()
                .eq("rs_rlzy_qyxx_lsh",dto.getRsrlzyqyxxlsh())
                .eq("rs_rlzy_ts_lsh",dto.getRsrlzytslsh())
                .and((obj)->obj.eq("mszt","1").or().eq("mszt","2")));
        if (cnt>0){
            throw new AppException("不能重复邀请！");
        }

        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUsername() == null ? "" : currentUser.getUsername().trim();
        DateTimeFormatter DATETIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String dateStr = LocalDateTime.now().format(DATETIME_FORMATTER);

        VRsRlzyMsglPO msglpo = new VRsRlzyMsglPO();
        BeanUtils.copyProperties(dto,msglpo);
        msglpo.setTsxgr(userid);

        VRsRlzyMsryglPO jlpo = new VRsRlzyMsryglPO();
        jlpo.setTsxgr(userid);
        jlpo.setTsxgsj(dateStr);

        if (StringUtils.isNotBlank(dto.getRsrlzymsgllsh())){
            //修改
            //首先先将之前勾选的面试官全部清空
            //写入新勾选的面试官
        } else {
            //新增
            msglpo.setTscjr(userid);
            msglpo.setTscjsj(dateStr);
            vRsRlzyMsglMapper.insert(msglpo);

            String rsrlzymsgllsh = msglpo.getRsrlzymsgllsh();

            String[] msgs = dto.getRsrlzymsggllsh().split(",");
            for (int i = 0; i < msgs.length; i++) {
                VRsRlzyMsryglPO msgpo = new VRsRlzyMsryglPO();
                msgpo.setTsxgr(userid);
                msgpo.setTscjsj(dateStr);
                vRsRlzyMsryglMapper.insert(msgpo);
            }

            jlpo.setRsrlzymsgllsh(rsrlzymsgllsh);
            jlpo.setTscjsj(dateStr);
            vRsRlzyMsryglMapper.insert(jlpo);
        }
    }

    @Override
    public PageResult getMsgllist(MsQueryDTO dto) {
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUsername() == null ? "" : currentUser.getUsername().trim();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userid",userid);
        RsRlzyQyxxPO po = qyxxMapper.selectOne(new QueryWrapper<RsRlzyQyxxPO>().select("rs_rlzy_qyxx_lsh").eq("rs_yhlsh",userid));
        if (po!=null && !("".equals(po.getRsrlzyqyxxlsh()))){
            params.put("rsrlzyqyxxlsh",po.getRsrlzyqyxxlsh());
        } else {
            throw new AppException("没有该企业信息");
        }
        Page<Map<String, Object>> page = new Page<>(dto.getPagenum(), dto.getPagesize());
        Page<Map<String, Object>> pageResult = (Page) vRsRlzyMsglMapper.getMsgllist(page, params);
        PageResult result = PageUtil.PageResult(pageResult);
        return result;
    }

    @Override
    public PageResult getMsgllistForGr(MsQueryDTO dto) {
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUsername() == null ? "" : currentUser.getUsername().trim();

        if(StringUtils.isBlank(userid)){
            throw new AppException("当前用户不存在！");
        }
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userid",userid);
        Page<Map<String, Object>> page = new Page<>(dto.getPagenum(), dto.getPagesize());
        Page<Map<String, Object>> pageResult = (Page) vRsRlzyMsglMapper.getMsgllistForGr(page, params);
        PageResult result = PageUtil.PageResult(pageResult);
        return result;
    }

//    @Override
//    public void updatemsjg(MsglZtUDTO dto) {
//        VRsRlzyMsglPO po = new VRsRlzyMsglPO();
//        po.setMsjg(dto.getFlag());
//        vRsRlzyMsglMapper.update(po,new UpdateWrapper<VRsRlzyMsglPO>().eq("rs_rlzy_msgl_lsh",dto.getRsrlzymsgllsh()));
//    }

    @Override
    public Map getMsglInfo(ZjDTO dto) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("zjlsh",dto.getZjlsh());
        return vRsRlzyMsglMapper.getMsglInfo(params);
    }

    @Override
    public Map getMsglInfoForYdd(ZjDTO dto) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("zjlsh",dto.getZjlsh());
        return vRsRlzyMsglMapper.getMsglInfoForYdd(params);
    }

    @Override
    public void updatemsgl(MsglZtUDTO dto) {
        VRsRlzyMsglPO po = new VRsRlzyMsglPO();
        BeanUtils.copyProperties(dto,po);
        vRsRlzyMsglMapper.updateById(po);

        QueryWrapper<VRsRlzyMsglPO> wrapper = new QueryWrapper<>();
        wrapper.eq("rs_rlzy_msgl_lsh", dto.getRsrlzymsgllsh());
        VRsRlzyMsglPO msglPO = vRsRlzyMsglMapper.selectOne(wrapper);
        String jwtToken = msglPO.getJwtToken();
        if("1".equals(po.getSfksms()) && StringUtils.isEmpty(jwtToken)) {
            Algorithm algorithm = Algorithm.HMAC256("sinobest_jitsi_meet");

            String token = JWT.create()
                    .sign(algorithm);

            po.setJwtToken(token);
            vRsRlzyMsglMapper.updateById(po);
        }
        //vRsRlzyMsglMapper.update(po,new UpdateWrapper<VRsRlzyMsglPO>().eq("rs_rlzy_msgl_lsh",dto.getRsrlzymsgllsh()));
    }

    @Override
    public PageResult getMsglMsglist(MsglZtListDTO dto) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id",dto.getRsrlzymsgllsh());
        Page<Map<String, Object>> page = new Page<>(dto.getPagenum(), dto.getPagesize());
        Page<Map<String, Object>> pageResult = (Page) vRsRlzyMsglMapper.getMsglMsglist(page, params);
        PageResult result = PageUtil.PageResult(pageResult);
        return result;
    }

    @Override
    public Map queryQyxx(MsglZtUDTO dto) {
        Map<String, Object> result = new HashMap<>();
        QueryWrapper<RsRlzyQyxxPO> qyxxPOQueryWrapper = new QueryWrapper<>();
        qyxxPOQueryWrapper.eq("rs_rlzy_qyxx_lsh" , dto.getRsrlzyqyxxlsh());
        RsRlzyQyxxPO qyxxPO = qyxxMapper.selectOne(qyxxPOQueryWrapper);

        QueryWrapper<RsRlzyZwxxPO> zwxxPOQueryWrapper = new QueryWrapper<>();
        zwxxPOQueryWrapper.eq("rs_rlzy_qyxx_lsh" , dto.getRsrlzyqyxxlsh())
                .eq("rs_rlzy_zwxx_lsh", dto.getRsrlzygwxxlsh());
        RsRlzyZwxxPO zwxxPO = zwxxMapper.selectOne(zwxxPOQueryWrapper);

        result.put("gsmc", qyxxPO.getGsmc());
        result.put("gzms", zwxxPO.getGzms());
        result.put("zprs", zwxxPO.getZprs());
        result.put("gzjy", zwxxPO.getGzjy());
        result.put("xz", zwxxPO.getXz());
        return result;
    }

    @Override
    public Map validMsjxx(MsyqglDTO dto) {
        Map<String, Object> result = new HashMap<>();
        QueryWrapper<VRsRlzyMsgglPO> vRsRlzyMsgglPOQueryWrapper = new QueryWrapper<>();
        vRsRlzyMsgglPOQueryWrapper.eq("rs_rlzy_msggl_lsh", dto.getRsrlzymsggllsh())
                .eq("xm", dto.getXm())
                .eq("zjhm", dto.getZjhm());
        VRsRlzyMsgglPO vRsRlzyMsgglPO = vRsRlzyMsgglMapper.selectOne(vRsRlzyMsgglPOQueryWrapper);
        if (vRsRlzyMsgglPO == null) {
            throw new AppException("面试官信息错误,请核准!");
        }
        QueryWrapper<VRsRlzyMsglPO> vRsRlzyMsglPOQueryWrapper = new QueryWrapper<>();
        vRsRlzyMsglPOQueryWrapper.eq("rs_rlzy_msgl_lsh", dto.getRsrlzymsgllsh());
        VRsRlzyMsglPO vRsRlzyMsglPO = vRsRlzyMsglMapper.selectOne(vRsRlzyMsglPOQueryWrapper);

        result.put("xm", vRsRlzyMsgglPO.getRsrlzyqyxxlsh());
        result.put("zjhm", vRsRlzyMsgglPO.getRsrlzyqyxxlsh());
        result.put("jwt_token", vRsRlzyMsglPO.getJwtToken());

        return result;
    }

    @Override
    public Map<Object, String> getMszInfo(String rsyhlsh) {
        return vRsRlzyMsglMapper.getMszInfo(rsyhlsh);
    }

    @Override
    public Map<Object, String> getMsqyInfo(String rsrlzyqyxxlsh) {
        return vRsRlzyMsglMapper.getMsqyInfo(rsrlzyqyxxlsh);
    }

    @Override
    public RsRlzyZwxxPO selectById(String rsrlzyzwxxlsh) {
        return zwxxMapper.selectById(rsrlzyzwxxlsh);
    }

    @Override
    public void updategwzt(String rsyhlsh, String rsrlzyzwxxlsh) {
        GwxxPO po = new GwxxPO();
        po.setGwzt("4");
        gwxxMapper.update(po,new UpdateWrapper<GwxxPO>().eq("rs_yhlsh",rsyhlsh)
                .eq("rs_rlzy_zwxx_lsh",rsrlzyzwxxlsh));
    }

    @Override
    public Map getJwtToken(MsglZtUDTO dto) {
        QueryWrapper<VRsRlzyMsglPO> vRsRlzyMsglPOQueryWrapper = new QueryWrapper<>();
        VRsRlzyMsglPO vRsRlzyMsglPO = vRsRlzyMsglMapper.selectOne(vRsRlzyMsglPOQueryWrapper);

        Map<String, String> result = new HashMap<>();
        result.put("jwt", vRsRlzyMsglPO.getJwtToken());

        return result;
    }


}



