package com.kiisoo.ic.synchronous.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kiisoo.aegis.common.faces.util.HttpUtil;
import com.kiisoo.ic.common.utils.MD5FileUtil;
import com.kiisoo.ic.common.utils.httpClientUtil.HttpClientUtil;
import com.kiisoo.ic.common.utils.httpClientUtil.HttpResult;
import com.kiisoo.ic.employee.entity.PrivilageCpUserDO;
import com.kiisoo.ic.employee.mapper.PrivilageCpUserDOMapper;
import com.kiisoo.ic.store.constant.Constants;
import com.kiisoo.ic.store.entity.PoiStore;
import com.kiisoo.ic.store.entity.PoiStoreStaff;
import com.kiisoo.ic.store.entity.PrivilageCpUserStoreDO;
import com.kiisoo.ic.store.entity.StoreUserDTO;
import com.kiisoo.ic.store.mapper.PoiStoreDOMapper;
import com.kiisoo.ic.store.mapper.PoiStoreStaffDOMapper;
import com.kiisoo.ic.store.mapper.PrivilageCpUserStoreDOMapper;
import com.kiisoo.ic.synchronous.entity.PromotionDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import static com.kiisoo.ic.employee.constant.Constants.DATABASE_CP_USER_ID_KEY;

@Service
public class PromotionCodeService {

    @Autowired
    private PoiStoreDOMapper poiStoreDOMapper;

    @Autowired
    private PrivilageCpUserStoreDOMapper privilageCpUserStoreDOMapper;

    @Autowired
    private PoiStoreStaffDOMapper poiStoreStaffDOMapper;

    @Autowired
    private PrivilageCpUserDOMapper privilageCpUserDOMapper;

    public String getToken(){
        String token = "BOSIDENG";
        String date = "2018-10-31";
        return MD5FileUtil.getMD5String(token+date);
    }

    /**
     * 找店铺二维码
     * @param i
     */
    public void getPromotion(int i) {
        //找到所有店铺
      List<PoiStore> shops = poiStoreDOMapper.selectList(null);

        shops.forEach(item ->{
            List<Long> shopIds = new ArrayList<>();
            shopIds.add(item.getId());
            //找到是否有推广码
            List<StoreUserDTO> cpUserStoreDO = privilageCpUserStoreDOMapper.selectCpUserByShops(shopIds);
            if(cpUserStoreDO.size() == 0){
                PromotionDO p = new PromotionDO();
                p.setType("0");
                p.setStoreNo(item.getCode());
                //没有推广码则查询
                HttpResult data = HttpClientUtil.httpPost("http://vtest.bsd.cn/BSD_WXAPP/qyWechat/getShareQRcode?token=TOKEN", JSON.toJSONString(p),getToken());
                //http返回参数
                if((boolean) JSONObject.parseObject(data.getMessage()).get("success")){
                    String url = JSONObject.parseObject(data.getMessage()).get("data").toString();

                    //排重---根据企业微信账号查询是否存在用户
                    PrivilageCpUserDO privilageCpUserDO = new PrivilageCpUserDO();
                    privilageCpUserDO.setQrCode(url);
                    int insert = privilageCpUserDOMapper.insert(privilageCpUserDO);
                    PrivilageCpUserStoreDO cpUserStore = new PrivilageCpUserStoreDO();
                    cpUserStore.setStoreId(item.getId());
                    cpUserStore.setCpUserId(privilageCpUserDO.getId());
                    privilageCpUserStoreDOMapper.insert(cpUserStore);
                }

            }


        });

    }

    /**
     * 找用户二维码
     */
    public void getStaffPromotion() {
        List<PoiStoreStaff> sellers = poiStoreStaffDOMapper.selectList(null);
        sellers.forEach(item ->{
            if(item.getEpWechatQrCode() == null){
                PromotionDO p = new PromotionDO();
                p.setUserId(item.getCustomerUserId()+"");
                p.setType("1");
                p.setState(item.getStaffCode());

                //没有推广码则查询
                HttpResult data = HttpClientUtil.httpPost("http://vtest.bsd.cn/BSD_WXAPP/qyWechat/getShareQRcode?token=TOKEN", JSON.toJSONString(p),getToken());
                //http返回参数
                if((boolean) JSONObject.parseObject(data.getMessage()).get("success")){
                    String url = JSONObject.parseObject(data.getMessage()).get("data").toString();

                    //没有推广码则查询
                    item.setEpWechatQrCode(url);
                    poiStoreStaffDOMapper.updateById(item);
                }

            }
        });

    }
}
