package com.hzs.sale.product.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
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.EDelFlag;
import com.hzs.common.core.enums.ETransactionKey;
import com.hzs.common.core.enums.EYesNo;
import com.hzs.common.core.service.RedisService;
import com.hzs.common.domain.sale.product.BdAreaClassify;
import com.hzs.common.domain.sale.product.BdAreaClassifySpecialArea;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.sale.product.controller.manage.params.AreaClassifyParam;
import com.hzs.sale.product.mapper.BdAreaClassifyMapper;
import com.hzs.sale.product.service.IBdAreaClassifyService;
import com.hzs.sale.product.service.IBdAreaClassifySpecialAreaService;
import com.hzs.sale.product.vo.AreaClassifyVo;
import com.hzs.system.base.ITransactionServiceApi;
import com.hzs.system.sys.dto.LoginUser;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 商品分类 服务实现类
 *
 * @author hzs
 * @since 2022-09-05
 */
@Service
public class BdAreaClassifyServiceImpl extends ServiceImpl<BdAreaClassifyMapper, BdAreaClassify> implements IBdAreaClassifyService {

    @Autowired
    private RedisService redisService;
    @Autowired
    private IBdAreaClassifyService areaClassifyService;
    @DubboReference
    private ITransactionServiceApi transactionServiceApi;
    @Autowired
    private IBdAreaClassifySpecialAreaService areaClassifySpecialAreaService;

    /**
     * 新增商品分类
     *
     * @param areaClassifyParam 分类对象
     * @param loginUser         登录用户
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveAreaClassify(AreaClassifyParam areaClassifyParam, LoginUser loginUser) {
        BdAreaClassify areaClassify = BeanUtil.copyProperties(areaClassifyParam, BdAreaClassify.class);
        areaClassify.setPkCreator(loginUser.getUserId());
        areaClassify.setCreationTime(new Date());
        areaClassify.setPkCountry(loginUser.getDataCountry());
        R<Integer> transaction = transactionServiceApi.createTransaction(loginUser.getDataCountry(), ETransactionKey.SPEC, areaClassify.getClassifyName(), EYesNo.NO, EYesNo.NO);
        areaClassify.setPkTransactionKey(transaction.getData());
        baseMapper.insert(areaClassify);
        List<BdAreaClassifySpecialArea> list = new ArrayList<>();
        if (areaClassifyParam.getSpecialAreaList() != null) {
            for (Integer number : areaClassifyParam.getSpecialAreaList()) {
                BdAreaClassifySpecialArea areaClassifySpecialArea = new BdAreaClassifySpecialArea();
                areaClassifySpecialArea.setSpecialArea(number);
                areaClassifySpecialArea.setPkAreaClassify(areaClassify.getPkId());
                areaClassifySpecialArea.setPkCreator(loginUser.getUserId());
                areaClassifySpecialArea.setCreationTime(new Date());
                areaClassifySpecialArea.setPkCountry(loginUser.getDataCountry());
                list.add(areaClassifySpecialArea);
            }
        }

        if (list.size() > 0) {
            areaClassifySpecialAreaService.saveBatch(list);
        }
    }

    /**
     * 修改商品分类
     *
     * @param areaClassifyParam 分类对象
     * @param loginUser         当前登录用户
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateAreaClassify(AreaClassifyParam areaClassifyParam, LoginUser loginUser) {
        BdAreaClassify areaClassify = BeanUtil.copyProperties(areaClassifyParam, BdAreaClassify.class);
        areaClassify.setPkModified(loginUser.getUserId());
        areaClassify.setModifiedTime(new Date());
        R<Integer> transaction = transactionServiceApi.createTransaction(loginUser.getDataCountry(), ETransactionKey.SPEC, areaClassify.getClassifyName(), EYesNo.NO, EYesNo.NO);
        areaClassify.setPkTransactionKey(transaction.getData());
        baseMapper.updateById(areaClassify);
        // 清空 中间表数据重新添加
        areaClassifySpecialAreaService.deleteByAreaClassifyPk(areaClassify.getPkId());
        List<BdAreaClassifySpecialArea> list = new ArrayList<>();
        if (areaClassifyParam.getSpecialAreaList() != null) {
            for (Integer number : areaClassifyParam.getSpecialAreaList()) {
                BdAreaClassifySpecialArea areaClassifySpecialArea = new BdAreaClassifySpecialArea();
                areaClassifySpecialArea.setSpecialArea(number);
                areaClassifySpecialArea.setPkAreaClassify(areaClassify.getPkId());
                areaClassifySpecialArea.setPkCreator(loginUser.getUserId());
                areaClassifySpecialArea.setCreationTime(new Date());
                areaClassifySpecialArea.setPkCountry(loginUser.getDataCountry());
                list.add(areaClassifySpecialArea);
            }
        }
        if (list.size() > 0) {
            areaClassifySpecialAreaService.saveBatch(list);
        }
        // 删除缓存
        String redisKey = CacheConstants.BD_AREA_CLASSIFY + areaClassify.getPkId();
        redisService.deleteObject(redisKey);
    }

    /**
     * 根据主键查询商品分类
     *
     * @param areaClassifyId
     * @return
     */
    @Override
    public BdAreaClassify getAreaClassify(Integer areaClassifyId) {
        String redisKey = CacheConstants.BD_AREA_CLASSIFY + areaClassifyId;
        BdAreaClassify areaClassify = redisService.getCacheObject(redisKey);
        if (null == areaClassify) {
            areaClassify = baseMapper.selectById(areaClassifyId);
            if (null != areaClassify) {
                redisService.setCacheObject(redisKey, areaClassify);
            }
        }
        return areaClassify;
    }

    /**
     * 查询商品分类列表
     *
     * @param areaClassifyParam
     * @return
     */
    @Override
    public List<AreaClassifyVo> selectByAreaClassifyList(AreaClassifyParam areaClassifyParam) {
        return baseMapper.selectByAreaClassifyList(areaClassifyParam);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAreaClassify(Integer pkId) {

        UpdateWrapper<BdAreaClassify> bdAreaClassify = new UpdateWrapper<>();
        bdAreaClassify.eq("PK_ID", pkId);
        bdAreaClassify.set("PK_MODIFIED", SecurityUtils.getUserId());
        bdAreaClassify.set("MODIFIED_TIME", new Date());
        bdAreaClassify.set("DEL_FLAG", EDelFlag.DELETE.getValue());
        areaClassifyService.update(bdAreaClassify);

        // 删除缓存
        String redisKey = CacheConstants.BD_AREA_CLASSIFY + pkId;
        redisService.deleteObject(redisKey);
        // 清空 中间表数据重新添加
        areaClassifySpecialAreaService.deleteByAreaClassifyPk(pkId);
    }

}
