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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.classify.BdSpecs;
import com.hzs.common.domain.sale.classify.BdSpecsType;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.sale.product.controller.manage.params.SpecsParam;
import com.hzs.sale.product.controller.manage.params.SpecsTypeParam;
import com.hzs.sale.product.mapper.BdSpecsTypeMapper;
import com.hzs.sale.product.service.IBdSpecsService;
import com.hzs.sale.product.service.IBdSpecsTypeService;
import com.hzs.sale.product.vo.SpecsTypeVo;
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;

/**
 * <p>
 * 规格类型表 服务实现类
 * </p>
 *
 * @author hzs
 * @since 2022-08-27
 */
@Service
public class BdSpecsTypeServiceImpl extends ServiceImpl<BdSpecsTypeMapper, BdSpecsType> implements IBdSpecsTypeService {
    @Autowired
    private RedisService redisService;
    @DubboReference
    private ITransactionServiceApi transactionServiceApi;
    @Autowired
    private IBdSpecsTypeService  specsTypeService;
    @Autowired
    private IBdSpecsService  specsService;
    /**
     * 根据id 查询规格分类
     * @return
     */
    @Override
    public BdSpecsType getSpecsType(Integer specsTypeId) {
        String redisKey = CacheConstants.BD_SPECS_TYPE + specsTypeId;
        BdSpecsType specsType = redisService.getCacheObject(redisKey);
        if (null == specsType) {
            specsType = baseMapper.selectById(specsTypeId);
            if (null != specsType) {
                redisService.setCacheObject(redisKey, specsType);
            }
        }
        return specsType;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveSpecsType(SpecsTypeParam specsTypeParam, LoginUser loginUser) {
        BdSpecsType bdSpecsType = new BdSpecsType();
        bdSpecsType.setPkClassify(specsTypeParam.getPkClassify());
        bdSpecsType.setTypeName(specsTypeParam.getTypeName());
        bdSpecsType.setPkCreator(loginUser.getUserId());
        bdSpecsType.setCreationTime(new Date());
        bdSpecsType.setSort(specsTypeParam.getSort());
        bdSpecsType.setPkCountry(loginUser.getDataCountry());
        R<Integer> transaction = transactionServiceApi.createTransaction(loginUser.getDataCountry(), ETransactionKey.SPEC, specsTypeParam.getTypeName(), EYesNo.NO,EYesNo.NO);
        bdSpecsType.setPkTransactionKey(transaction.getData());
        baseMapper.insert(bdSpecsType);
        List<BdSpecs> specsList = new ArrayList<>();
        for (SpecsParam specs : specsTypeParam.getSpecsList()) {
            BdSpecs bdSpecs = new BdSpecs();
            bdSpecs.setPkSpecsType(bdSpecsType.getPkId());
            bdSpecs.setSpecsName(specs.getValue());
            bdSpecs.setPkCreator(SecurityUtils.getUserId());
            bdSpecs.setCreationTime(new Date());
            bdSpecs.setPkCountry(SecurityUtils.getPkCountry());
            R<Integer> trans = transactionServiceApi.createTransaction(loginUser.getDataCountry(), ETransactionKey.SPEC, bdSpecs.getSpecsName(), EYesNo.NO,EYesNo.NO);
            bdSpecs.setPkTransactionKey(trans.getData());
            specsList.add(bdSpecs);
        }

        specsService.saveBatch(specsList);
        String redisKey = CacheConstants.BD_SPECS_TYPE + bdSpecsType.getPkId();
        redisService.deleteObject(redisKey);
    }

    /**
     * 修改规格分类
     * @param specsTypeParam
     * @param loginUser
     */
    @Override
    public void updateSpecsType(SpecsTypeParam specsTypeParam, LoginUser loginUser) {
        BdSpecsType bdSpecsType = new BdSpecsType();
        BdSpecsType st = baseMapper.selectById(specsTypeParam.getPkId());
        bdSpecsType.setPkId(specsTypeParam.getPkId());
        bdSpecsType.setPkClassify(specsTypeParam.getPkClassify());
        bdSpecsType.setModifiedTime(new Date());
        bdSpecsType.setPkModified(loginUser.getUserId());
        bdSpecsType.setTypeName(specsTypeParam.getTypeName());
        bdSpecsType.setSort(specsTypeParam.getSort());
     //   transactionServiceApi.updateTransaction(loginUser.getDataCountry(),specsTypeParam.getTypeName(),st.getPkTransactionKey(),true );
       // bdSpecsType.setPkTransactionKey(transaction.getData());
        baseMapper.updateById(bdSpecsType);

        List<Integer> specsId = new ArrayList<>();
        for (SpecsParam specsParam : specsTypeParam.getSpecsList()) {
            if (specsParam.getSpecsId() != null){
                specsId.add(specsParam.getSpecsId());
            }
        }
        specsService.remove(new QueryWrapper<BdSpecs>().notIn("PK_ID",specsId).eq("PK_SPECS_TYPE",specsTypeParam.getPkId()));

        List<BdSpecs> specsList = new ArrayList<>();
        for (SpecsParam specs : specsTypeParam.getSpecsList()) {
            BdSpecs bdSpecs = new BdSpecs();
             if (specs.getSpecsId() != null){
                 BdSpecs sp = specsService.getById(specs.getSpecsId());
                 sp.setSpecsName(specs.getValue());
                 sp.setModifiedTime(new Date());
                 sp.setPkModified(SecurityUtils.getUserId());
                 sp.setDelFlag(EDelFlag.UN_DELETE.getValue());
                 specsService.updateById(sp);
             }else {
                 bdSpecs.setPkSpecsType(bdSpecsType.getPkId());
                 bdSpecs.setSpecsName(specs.getValue());
                 bdSpecs.setPkCreator(SecurityUtils.getUserId());
                 bdSpecs.setCreationTime(new Date());
                 bdSpecs.setPkCountry(SecurityUtils.getPkCountry());
                 R<Integer> trans = transactionServiceApi.createTransaction(loginUser.getDataCountry(), ETransactionKey.SPEC, bdSpecs.getSpecsName(),EYesNo.NO,EYesNo.NO);
                 bdSpecs.setPkTransactionKey(trans.getData());
                 specsList.add(bdSpecs);
             }


        }
        if (specsList.size() > 0){
            specsService.saveBatch(specsList);
        }

        String redisKey = CacheConstants.BD_SPECS_TYPE + bdSpecsType.getPkId();
        redisService.deleteObject(redisKey);
    }
    /**
     * 查询列表
     * @param specsType
     * @return
     */
    @Override
    public List<SpecsTypeVo> selectSpecsTypeList(BdSpecsType specsType) {
        return baseMapper.selectSpecsTypeList(specsType);

    }
    /**
     * 删除
     * @param pkId
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBySpecsTypeId(Integer pkId,LoginUser loginUser) {
        UpdateWrapper<BdSpecsType> updateWrapper = new UpdateWrapper();
        updateWrapper.eq("PK_ID",pkId);
        updateWrapper.set("DEL_FLAG",EDelFlag.DELETE.getValue());
        updateWrapper.set("MODIFIED_TIME",new Date());
        updateWrapper.set("PK_MODIFIED",loginUser.getUserId());
        specsTypeService.update(updateWrapper);
        UpdateWrapper<BdSpecs> updateSpecsWrapper = new UpdateWrapper<>();
        updateSpecsWrapper.eq("PK_SPECS_TYPE",pkId);
        updateSpecsWrapper.set("MODIFIED_TIME",new Date());
        updateSpecsWrapper.set("PK_MODIFIED",loginUser.getUserId());
        updateSpecsWrapper.set("DEL_FLAG", EDelFlag.DELETE.getValue());
        specsService.update(updateSpecsWrapper);
        String redisKey = CacheConstants.BD_SPECS_TYPE + pkId;
        redisService.deleteObject(redisKey);
    }
}
