package com.kiisoo.ic.employee.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kiisoo.ic.config.WxCpConfiguration;
import com.kiisoo.ic.employee.entity.QrCodeDO;
import com.kiisoo.ic.employee.entity.QrCodeVO;
import com.kiisoo.ic.store.entity.PoiStoreStaff;
import com.kiisoo.ic.store.mapper.PoiStoreStaffDOMapper;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.api.WxCpService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

import static com.kiisoo.ic.config.WxCpConfiguration.APPLICATIONID;

/**
 * @Description: 推广码业务
 * @Auther: yinliujing
 * @Date: 2020/4/7 0002 14:06
 * @Version: v1
 */
@Service
public class QrCodeService {
    WxCpService mainService = WxCpConfiguration.getCpService(APPLICATIONID);
    @Autowired
    private PoiStoreStaffDOMapper poiStoreStaffDOMapper;

    public QrCodeVO getQrCode(QrCodeDO qrCodeDO) throws Exception {
        //获取token
        String token = mainService.getAccessToken();
        qrCodeDO.setAccess_token(token);
        //获取configId
        String configId = getConfigId(qrCodeDO);
        //获取配置实体
        QrCodeVO qrCodeVO = getContactWay(token, configId);
        //添加店铺人员
        //saveStoreStaff( parseStoreStaff(qrCodeDO,qrCodeVO));
        return qrCodeVO;
    }

    /**
     * 配置客户联系「联系我」方式
     *
     * @param qrCodeDO
     * @return
     * @throws WxErrorException
     */
    public String getConfigId(QrCodeDO qrCodeDO) throws WxErrorException {
        String url = mainService.getWxCpConfigStorage().getApiUrl("/cgi-bin/externalcontact/add_contact_way");
        String result = mainService.post(url, qrCodeDO.toJson());
        JSONObject parseObject = JSON.parseObject(result);
        String configId = parseObject.getString("config_id");
        return configId;
    }

    /**
     * 获取企业已配置的「联系我」方式
     *
     * @param token
     * @param configId
     * @return
     * @throws WxErrorException
     */
    public QrCodeVO getContactWay(String token, String configId) throws WxErrorException {
        String url = mainService.getWxCpConfigStorage().getApiUrl("/cgi-bin/externalcontact/get_contact_way");
        Map<String, Object> map = new HashMap<>();
        map.put("config_id", configId);
        map.put("access_token", token);
        String result = mainService.post(url, JSON.toJSONString(map));
        JSONObject parseObject = JSON.parseObject(result);
        QrCodeVO qrCodeVO = JSONObject.parseObject(parseObject.getString("contact_way"), QrCodeVO.class);
        return qrCodeVO;
    }


    /***
     *封装需要添加的店铺人员实体
     *  前台传的实体参数
     *  企业微信返回的实体
     */
    public PoiStoreStaff parseStoreStaff(QrCodeDO qrCodeDO,QrCodeVO qrCodeVO) throws Exception {
        PoiStoreStaff poiStoreStaff = new PoiStoreStaff();
        BeanUtils.copyProperties(qrCodeDO,poiStoreStaff);
        poiStoreStaff.setType(qrCodeDO.getRole());
        poiStoreStaff.setEpWechatQrCode(qrCodeVO.getQr_code());
        poiStoreStaff.setEpWechatConfigId(qrCodeVO.getConfig_id());
        return  poiStoreStaff;
    }
    /**
     * 添加店铺人员表
     * @return
     * @throws WxErrorException
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveStoreStaff(PoiStoreStaff poiStoreStaffDO) throws Exception {
        QueryWrapper<PoiStoreStaff> storeStaff = new QueryWrapper<>();
        storeStaff.eq("user_id",poiStoreStaffDO.getUserId()).eq("store_id",poiStoreStaffDO.getStoreId()).last("limit 1");
        PoiStoreStaff poiStoreStaff = poiStoreStaffDOMapper.selectOne(storeStaff);
        if (null == poiStoreStaff){
            poiStoreStaffDOMapper.insert(poiStoreStaffDO);
        }
    }
}
