package com.hzs.system.config.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.constant.CacheConstants;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.enums.EShop;
import com.hzs.common.core.enums.ETransactionKey;
import com.hzs.common.core.enums.EYesNo;
import com.hzs.common.core.enums.EnableStatus;
import com.hzs.common.core.service.RedisService;
import com.hzs.common.domain.system.config.BdShop;
import com.hzs.common.domain.system.config.ext.BdShopExt;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.system.base.ITransactionServiceApi;
import com.hzs.system.config.mapper.BdShopMapper;
import com.hzs.system.config.service.IBdShopService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 *  店铺配置 服务实现类
 * </p>
 *
 * @author zhangjing
 * @since 2022-09-15
 */
@Service
public class BdShopServiceImpl extends ServiceImpl<BdShopMapper, BdShop> implements IBdShopService {

    @DubboReference
    private ITransactionServiceApi iTransactionServiceApi;
    private RedisService redisService;

    @Autowired
    public void setRedisService(RedisService redisService) {
        this.redisService = redisService;
    }

    /**
     * @description: 生成配送中心
     * @author: zhang jing
     * @date: 2022/10/28 18:38
     * @param: [shop]
     * @return: boolean
     **/
    @Override
    public boolean generate(BdShop shop) {
        Integer pkCountry= SecurityUtils.getPkCountry();
        for (EShop value : EShop.values()) {
            shop.setShopValue(value.getValue());
            shop.setShopName(value.getLabel());
            //字段翻译保存到数据库
            R<Integer> pkTransaction = iTransactionServiceApi.createTransaction(pkCountry, ETransactionKey.GRADE,
                    value.getLabel(), EYesNo.YES, EYesNo.NO);
            shop.setPkTransaction(pkTransaction.getData());
            shop.setPkCountry(SecurityUtils.getPkCountry());
            shop.setPkCreator(SecurityUtils.getUserId());
            shop.setCreationTime(new Date());
            QueryWrapper<BdShop> queryWrapper=new QueryWrapper();
            queryWrapper.eq("SHOP_VALUE",value.getValue());
            List<BdShop> list =baseMapper.selectList(queryWrapper);
            if(CollectionUtil.isEmpty(list)){
                baseMapper.insert(shop);
                redisService.deleteObjectByKey(CacheConstants.BD_SHOP + pkCountry);
            }
        }
        return true;
    }

    /**
     * @description: 根据国家查询配送中心列表
     * @author: zhang jing
     * @date: 2022/11/11 19:44
     * @param: [pkCountry]
     * @return: java.util.List<com.hzs.common.domain.system.config.ext.BdShopExt>
     **/
    @Override
    public List<BdShopExt> queryShop(Integer pkCountry) {
        List<BdShopExt> shopExts = redisService.getCacheList(CacheConstants.BD_SHOP + pkCountry);
        List<BdShopExt> shopExtsNew=new ArrayList<>();
        if(CollectionUtil.isEmpty(shopExts)){
            QueryWrapper<BdShop> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("PK_COUNTRY",pkCountry);
            queryWrapper.eq("ENABLE_STATE", EnableStatus.ENABLE.getValue());
            List<BdShop> btList = baseMapper.selectList(queryWrapper);
            if(CollectionUtil.isNotEmpty(btList)){
                btList.forEach(item -> shopExtsNew.add(BeanUtil.copyProperties(item, BdShopExt.class)));
                redisService.setCacheList(CacheConstants.BD_SHOP + pkCountry, shopExtsNew);
            }
            return shopExtsNew;
        }else{
            return shopExts;
        }
    }
}
