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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.hzs.common.core.constant.CacheConstants;
import com.hzs.common.core.enums.EDelFlag;
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.sys.dto.LoginUser;
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;

/**
 * 规格类型表 服务实现类
 */
@Service
public class BdSpecsTypeServiceImpl extends ServiceImpl<BdSpecsTypeMapper, BdSpecsType> implements IBdSpecsTypeService {

    @Autowired
    private RedisService redisService;
    @Autowired
    private IBdSpecsTypeService iBdSpecsTypeService;
    @Autowired
    private IBdSpecsService iBdSpecsService;

    @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());
        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());
            specsList.add(bdSpecs);
        }

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

    @Override
    public void updateSpecsType(SpecsTypeParam specsTypeParam, LoginUser loginUser) {
        BdSpecsType bdSpecsType = new BdSpecsType();
        bdSpecsType.setPkId(specsTypeParam.getPkId());
        bdSpecsType.setPkClassify(specsTypeParam.getPkClassify());
        bdSpecsType.setModifiedTime(new Date());
        bdSpecsType.setPkModified(loginUser.getUserId());
        bdSpecsType.setTypeName(specsTypeParam.getTypeName());
        bdSpecsType.setSort(specsTypeParam.getSort());
        baseMapper.updateById(bdSpecsType);

        List<Integer> specsId = new ArrayList<>();
        for (SpecsParam specsParam : specsTypeParam.getSpecsList()) {
            if (specsParam.getSpecsId() != null) {
                specsId.add(specsParam.getSpecsId());
            }
        }
        iBdSpecsService.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 = iBdSpecsService.getById(specs.getSpecsId());
                sp.setSpecsName(specs.getValue());
                sp.setModifiedTime(new Date());
                sp.setPkModified(SecurityUtils.getUserId());
                sp.setDelFlag(EDelFlag.UN_DELETE.getValue());
                iBdSpecsService.updateById(sp);
            } else {
                bdSpecs.setPkSpecsType(bdSpecsType.getPkId());
                bdSpecs.setSpecsName(specs.getValue());
                bdSpecs.setPkCreator(SecurityUtils.getUserId());
                bdSpecs.setCreationTime(new Date());
                bdSpecs.setPkCountry(SecurityUtils.getPkCountry());
                specsList.add(bdSpecs);
            }
        }
        if (specsList.size() > 0) {
            iBdSpecsService.saveBatch(specsList);
        }
        String redisKey = CacheConstants.BD_SPECS_TYPE + bdSpecsType.getPkId();
        redisService.deleteObject(redisKey);
    }

    @Override
    public List<SpecsTypeVo> selectSpecsTypeList(BdSpecsType specsType) {
        return baseMapper.selectSpecsTypeList(specsType);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBySpecsTypeId(Integer pkId, LoginUser loginUser) {
        LambdaUpdateWrapper<BdSpecsType> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(BdSpecsType::getPkId, pkId);
        updateWrapper.set(BdSpecsType::getDelFlag, EDelFlag.DELETE.getValue());
        updateWrapper.set(BdSpecsType::getModifiedTime, new Date());
        updateWrapper.set(BdSpecsType::getPkModified, loginUser.getUserId());
        iBdSpecsTypeService.update(updateWrapper);
        LambdaUpdateWrapper<BdSpecs> updateSpecsWrapper = new LambdaUpdateWrapper<>();
        updateSpecsWrapper.eq(BdSpecs::getPkSpecsType, pkId);
        updateSpecsWrapper.set(BdSpecs::getModifiedTime, new Date());
        updateSpecsWrapper.set(BdSpecs::getPkModified, loginUser.getUserId());
        updateSpecsWrapper.set(BdSpecs::getDelFlag, EDelFlag.DELETE.getValue());
        iBdSpecsService.update(updateSpecsWrapper);
        String redisKey = CacheConstants.BD_SPECS_TYPE + pkId;
        redisService.deleteObject(redisKey);
    }
}
