package com.shkj.wms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shkj.common.constant.RedisConstant;
import com.shkj.common.core.domain.Result;
import com.shkj.wms.bo.BaseBarcodesOutAddBo;
import com.shkj.wms.bo.BaseDeriveAddBo;
import com.shkj.wms.bo.BaseDeriveEditBo;
import com.shkj.wms.bo.BaseDeriveQueryBo;
import com.shkj.wms.domain.BaseBarcodesOut;
import com.shkj.wms.domain.BaseDerive;
import com.shkj.wms.domain.BaseGoodsData;
import com.shkj.wms.mapper.BaseDeriveMapper;
import com.shkj.wms.service.IBaseDeriveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 派生保险杠关系Service业务层处理
 *
 * @author zjx
 * @date 2023-04-13
 */

@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class BaseDeriveServiceImpl extends ServiceImpl<BaseDeriveMapper, BaseDerive> implements IBaseDeriveService {
    @Resource
    private BaseDeriveMapper baseDeriveMapper;

    @Autowired
    RedisTemplate redisTemplate;

    /**
     * 查询派生保险杠关系
     *
     * @param id 派生保险杠关系主键
     * @return 派生保险杠关系
     */
    @Override
    public BaseDerive selectBaseDeriveById(Long id)
    {
        return baseDeriveMapper.selectBaseDeriveById(id);
    }

    /**
     * 查询派生保险杠关系列表
     *
     * @param baseDerive 派生保险杠关系
     * @return 派生保险杠关系
     */
    @Override
    public List<BaseDerive> selectBaseDeriveList(BaseDerive baseDerive)
    {
        return baseDeriveMapper.selectBaseDeriveList(baseDerive);
    }

    /**
     * 新增派生保险杠关系
     *
     * @param bo 派生保险杠关系
     * @return 结果
     */
    @Override
    public Result<Boolean> insertBaseDerive(BaseDeriveAddBo bo)
    {
        LambdaQueryWrapper<BaseDerive> wrapper = new LambdaQueryWrapper<BaseDerive>().eq(BaseDerive::getDerive, bo.getDerive())
                .eq(BaseDerive::getVehicheCode,bo.getVehicheCode()).last("limit 1");
        BaseDerive one = getOne(wrapper);
        if (one != null) {
            return Result.err().msg("派生关系已经存在");
        }

        baseDeriveMapper.insert(BeanUtil.toBean(bo,BaseDerive.class));
        return Result.ok();
    }

    /**
     * 修改派生保险杠关系
     *
     * @param bo 派生保险杠关系
     * @return 结果
     */
    @Override
    public Result<Boolean> updateBaseDerive(BaseDeriveEditBo bo)
    {
         baseDeriveMapper.updateById(BeanUtil.toBean(bo,BaseDerive.class));
         return  Result.ok();
    }



    /**
     * 删除派生保险杠关系信息
     *
     * @param id 派生保险杠关系主键
     * @return 结果
     */
    @Override
    public int deleteBaseDeriveById(Long id)
    {
        return baseDeriveMapper.deleteById(id);
    }

    @Override
    public List<BaseDerive> getBaseDeriveListByVehiche(BaseBarcodesOutAddBo bo) {

        String redisDerive = bo.getDerive()+bo.getVehicleCode()+bo.getColor();
        List<BaseDerive> baseDericeList =null;
        baseDericeList =(List<BaseDerive>)redisTemplate.opsForValue().get(RedisConstant.redisDerive +redisDerive);

        if (null ==baseDericeList){
            baseDericeList = baseDeriveMapper.selectBaseDeriveListByVehiche(bo);
        }

        redisTemplate.opsForValue().set(RedisConstant.redisDerive + redisDerive,baseDericeList);
        return baseDericeList;

    }

    @Override
    public BaseDerive getBaseDeriveBybarcodes(BaseDeriveQueryBo bo) {
        return baseDeriveMapper.selectBaseDeriveBybarcodes(bo);
    }

    @Override
    public List<String> getVehicheModelInfo() {
        return baseDeriveMapper.getVehicheModelInfo();
    }

    @Override
    public List<String> getVehicheCodeInfo(BaseDeriveQueryBo bo) {

        return baseDeriveMapper.getVehicheCodeInfo(bo);
    }

    @Override
    public List<String> getColorInfo(BaseDeriveQueryBo bo) {
        return baseDeriveMapper.getColorInfo(bo);
    }

    @Override
    public List<String> getDeriveInfo(BaseDeriveQueryBo bo) {
        return baseDeriveMapper.getDeriveInfo(bo);
    }

    @Override
    public List<BaseGoodsData> getGoodsInfoByDerive(BaseDeriveQueryBo bo) {
        return baseDeriveMapper.getGoodsInfoByDerive(bo);
    }


}
