package com.kiisoo.ic.synchronous.service;

import com.alibaba.fastjson.JSONObject;
import com.kiisoo.ic.common.utils.MD5FileUtil;
import com.kiisoo.ic.common.utils.httpClientUtil.HttpClientUtil;
import com.kiisoo.ic.employee.entity.PrivilageCpUserDO;
import com.kiisoo.ic.employee.mapper.PrivilageCpUserDOMapper;
import com.kiisoo.ic.store.bean.BsdShareCodeResponse;
import com.kiisoo.ic.store.entity.PoiStore;
import com.kiisoo.ic.store.entity.PoiStoreStaff;
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 org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

import static com.kiisoo.ic.employee.constant.Constants.DATABASE_CP_USER_ID_KEY;
import static com.kiisoo.ic.store.constant.Constants.BSD_REQ_QRCODE_TYPE_SELLER;
import static com.kiisoo.ic.store.constant.Constants.BSD_REQ_QRCODE_TYPE_STORE;

@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);
    }

    @Value("${bsd.qrCode}")
    private String qrCodeBsdUrl;

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

        shops.forEach(item ->{
            List<Long> shopIds = new ArrayList<>();
            shopIds.add(item.getId());
            //找到是否有推广码
            List<StoreUserDTO> privilageCpUserDO = privilageCpUserStoreDOMapper.selectCpUserByShops(shopIds);
            if(privilageCpUserDO.size() > 0){
                StoreUserDTO cpUserStore = privilageCpUserDO.get(0);

                String qrCodeAction = cpUserStore.getQrCodeAction();
                if(StringUtils.isBlank(qrCodeAction)){
                    try {
                        qrCodeAction = getQrCodeFromBSD(cpUserStore.getCpUserId(),BSD_REQ_QRCODE_TYPE_STORE,"0",item.getCode());
                        cpUserStore.setQrCodeAction(qrCodeAction);
                        PrivilageCpUserDO update = new PrivilageCpUserDO();
                        update.setId(cpUserStore.getUserId());
                        update.setQrCode(qrCodeAction);
                        privilageCpUserDOMapper.updateById(update);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

            }


        });

    }
    /**
     * 获取波司登二维码
     * @param cpUserId
     * @param type
     * @param state
     * @param storeNo
     * @return
     * @throws Exception
     */
    public String getQrCodeFromBSD(String cpUserId,String type,String state,String storeNo) throws Exception {
        String url = "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Map<String, Object> params = new HashMap<>();
        params.put("userId", cpUserId);
        params.put("type", type);
        params.put("state", state);
        params.put("storeNo", storeNo);
        String message = HttpClientUtil.httpPost(qrCodeBsdUrl + MD5FileUtil.getMD5String("BOSIDENG" + sdf.format(new Date())), JSONObject.toJSONString(params)).getMessage();
        JSONObject qrCodeJson = JSONObject.parseObject(message);
        BsdShareCodeResponse bsdResponse = qrCodeJson.toJavaObject(BsdShareCodeResponse.class);
        if (bsdResponse.getSuccess() && StringUtils.isNotBlank(bsdResponse.getData())){
            url = bsdResponse.getData();
        }
        return null;
    }


    /**
     * 找用户二维码
     */
    public void getStaffPromotion() {
        List<PoiStoreStaff> sellers = poiStoreStaffDOMapper.selectList(null);
        sellers.forEach(item ->{
            if(item.getEpWechatQrCode() == null){
                String cpUserId = privilageCpUserStoreDOMapper.selectCpUserIdByStoreId(item.getStoreId());
                String url = null;
                try {
                    url = getQrCodeFromBSD(cpUserId, BSD_REQ_QRCODE_TYPE_SELLER, item.getStaffCode(), item.getStoreCode());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                //没有推广码则查询
                item.setEpWechatQrCode(url);
                poiStoreStaffDOMapper.updateById(item);
            }
        });

    }
}
