package cn.ysk.cashier.service.impl;

import cn.hutool.http.HttpUtil;
import cn.ysk.cashier.dto.BindingDto;
import cn.ysk.cashier.exception.BadRequestException;
import cn.ysk.cashier.pojo.shop.TbMerchantAccount;
import cn.ysk.cashier.pojo.shop.TbShopInfo;
import cn.ysk.cashier.repository.shop.TbMerchantAccountRepository;
import cn.ysk.cashier.repository.shop.TbShopInfoRepository;
import cn.ysk.cashier.service.BindService;
import cn.ysk.cashier.utils.CacheKey;
import cn.ysk.cashier.utils.RedisUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

@Service
public class BindServiceImpl implements BindService {

    @Autowired
    private TbMerchantAccountRepository accountRepository;
    @Autowired
    private TbShopInfoRepository tbShopInfoRepository;
    @Lazy
    @Autowired
    private RedisUtils redisUtils;
    private final String url = "http://test.invoice.sxczgkj.cn/api/";

    @Override
    public JSONObject binding(Map<String, Object> params) {
        String result = HttpUtil.post(url + "cash/binding", params, 5000);
        JSONObject jsonObject = JSONObject.parseObject(result);
        if (jsonObject.getInteger("code").equals(1)) {
            return jsonObject.getJSONObject("data");
        } else {
            throw new BadRequestException(jsonObject.getString("msg"));
        }
    }

    @Override
    public JSONObject subInvoicing(Map<String, Object> params) {
        String result = HttpUtil.post(url + "cash/subinvoicing", params, 5000);
        JSONObject jsonObject = JSONObject.parseObject(result);
        if (jsonObject.getInteger("code").equals(1)) {
            if (params.get("shopId") != null) {
                if (redisUtils.hasKey(CacheKey.INVOICE_SD_TYPE + params.get("shopId"))) {
                    Set<Object> articles = redisUtils.sGet(CacheKey.INVOICE_SD_TYPE + params.get("shopId"));
                    if (!articles.contains(params.get("article"))) {
                        articles.add(params.get("article"));
                        redisUtils.sSet(CacheKey.INVOICE_SD_TYPE + params.get("shopId"), articles);
                    }
                }
            }
            return jsonObject.getJSONObject("data");
        } else {
            throw new BadRequestException(jsonObject.getString("msg"));
        }
    }

    @Override
    public JSONObject industry(BindingDto bindingDto) {
        if (StringUtils.isNotBlank(bindingDto.getArticle())) {
            Map<String, Object> params = new HashMap<>();
            params.put("name", bindingDto.getArticle());
            params.put("page", bindingDto.getPage() == null ? 1 : bindingDto.getPage());
            String result = HttpUtil.post(url + "store/industrylistsj", params, 5000);
            JSONObject jsonObject = JSONObject.parseObject(result);
            if (jsonObject.getInteger("code").equals(1)) {
                return jsonObject.getJSONObject("data");
            } else {
                throw new BadRequestException(jsonObject.getString("msg"));
            }
        } else {
            Set<String> set = new HashSet<>();
            JSONObject jsonObject = new JSONObject();
            if (!redisUtils.hasKey(CacheKey.INVOICE_SD_TYPE + bindingDto.getShopId())) {
                TbShopInfo tbShopInfo = tbShopInfoRepository.findById(bindingDto.getShopId()).orElseGet(null);
                TbMerchantAccount account = accountRepository.findByAccount(tbShopInfo.getAccount());
                set.add(account.getArticle());
                redisUtils.sSet(CacheKey.INVOICE_SD_TYPE + bindingDto.getShopId(), set);
                jsonObject.put("articles", set);
            } else {
                jsonObject.put("list", redisUtils.sGet(CacheKey.INVOICE_SD_TYPE + bindingDto.getShopId()));
            }
            return jsonObject;
        }
    }

    @Override
    public JSONObject storeSe(Map<String, Object> params) {
        String result = HttpUtil.post(url + "store/se", params, 5000);
        JSONObject jsonObject = JSONObject.parseObject(result);
        if (jsonObject.getInteger("code").equals(1)) {
            return jsonObject.getJSONObject("data");
        } else {
            throw new BadRequestException(jsonObject.getString("msg"));
        }
    }

    @Override
    public JSONArray digitalInvoice() {
        String result = HttpUtil.post(url + "store/digitalinvoice", "", 5000);
        JSONObject jsonObject = JSONObject.parseObject(result);
        if (jsonObject.getInteger("code").equals(1)) {
            return jsonObject.getJSONArray("data");
        } else {
            throw new BadRequestException(jsonObject.getString("msg"));
        }
    }
}
