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.common.core.domain.entity.BaseConsignor;
import com.shkj.common.utils.StringUtils;
import com.shkj.system.service.ISysParameterService;
import com.shkj.wms.bo.BaseBarcodesInQueryBo;
import com.shkj.wms.bo.BaseGoodsAddBo;
import com.shkj.wms.bo.BaseGoodsEditBo;
import com.shkj.wms.constants.BusinessConstants;
import com.shkj.wms.constants.ParameterConstants;
import com.shkj.wms.domain.BaseDerive;
import com.shkj.wms.domain.BaseGoodsData;
import com.shkj.wms.mapper.BaseGoodsDataMapper;
import com.shkj.wms.service.IBaseConsignorService;
import com.shkj.wms.service.IBaseGoodsDataService;
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-06
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class BaseGoodsDataServiceImpl extends ServiceImpl<BaseGoodsDataMapper, BaseGoodsData> implements IBaseGoodsDataService
{
    @Resource
    private BaseGoodsDataMapper baseGoodsDataMapper;

    @Resource
    RedisTemplate redisTemplate;

    @Autowired
    IBaseGoodsDataService iBaseGoodsDataService;

    @Resource
    ISysParameterService sysParameterService;

    @Resource
    IBaseConsignorService baseConsignorService;

    @Override
    public String  insertByAddBo(BaseGoodsAddBo bo) {
        LambdaQueryWrapper<BaseGoodsData> wrapper = new LambdaQueryWrapper<BaseGoodsData>().eq(BaseGoodsData::getGoodsAllValue,bo.getGoodsAllValue()).last("limit 1");
        BaseGoodsData one = getOne(wrapper);
        BaseGoodsData baseGoods;

        if (one == null){
            baseGoods = BeanUtil.toBean(bo, BaseGoodsData.class);
            this.save(baseGoods);
            return baseGoods.getGoodsNo();
        }else {
            return one.getGoodsNo();
        }

    }

    @Override
    public Result<Boolean> updateByEditBo(BaseGoodsEditBo bo) {
        return null;
    }

    @Override
    public BaseGoodsData getBaseGoodsByBarcodes(String goodsNo) {
       // String goodsNo = bo.getColor() + bo.getVehicheModel() + bo.getLocation() + bo.getIsSplit() + bo.getIsPunching() + bo.getOrientation();

        //判断产品是否已经存在，如果已经存在，则不再新增；先从redis中查找
        BaseGoodsData  baseGoodsData = (BaseGoodsData) redisTemplate.opsForValue().get(RedisConstant.redisGoods + goodsNo);
        if (baseGoodsData == null) {
            LambdaQueryWrapper<BaseGoodsData> wrapperBaseGoods = new LambdaQueryWrapper<BaseGoodsData>().eq(BaseGoodsData::getGoodsNo, goodsNo).last("limit 1");
            baseGoodsData = iBaseGoodsDataService.getOne(wrapperBaseGoods);
        }
        return baseGoodsData;
    }

    @Override
    public List<String> getBumperColor() {
       return baseGoodsDataMapper.getBumperColor();
    }

    @Override
    public List<String> getBumperVehicheModel() {
        return  baseGoodsDataMapper.getBumperVehicheModel();
    }


    @Override
    public List<String> getBumperDerive() {
        return  baseGoodsDataMapper.getBumperDerive();
    }

    @Override
    public Result<BaseConsignor> getErpConsignorId() {
        String config = sysParameterService.selectErpConfigByKey(ParameterConstants.ParameterThird.consignorId);
        if (StringUtils.isBlank(config) || !StringUtils.isNumeric(config)) {
            return Result.err().msg(BusinessConstants.ThirdError.errorMsg);
        }
        Long consignorId = Long.parseLong(config);
        BaseConsignor consignor = baseConsignorService.getById(consignorId);
        if (consignor == null) {
            log.error("货主不存在 = {}", config);
            return Result.err().msg(BusinessConstants.ThirdError.errorMsg);
        }
        return Result.ok().data(consignor);
    }
    @Override
    public List<String> getBumperLocationCode() {
        return  baseGoodsDataMapper.getBumperLocationCode();
    }
    @Override
    public List<BaseGoodsData> getbaseGooddata(BaseGoodsData goodsData){
        LambdaQueryWrapper<BaseGoodsData> queryWrapper = new LambdaQueryWrapper<BaseGoodsData>()
                .eq(BaseGoodsData::getVehicheModel, goodsData.getVehicheModel())
                .eq(BaseGoodsData::getColor, goodsData.getColor())
                .eq(BaseGoodsData::getLocation, goodsData.getLocation()).eq(BaseGoodsData::getIsPunching, goodsData.getIsPunching())
                .eq(BaseGoodsData::getLocation, goodsData.getIsSplit()).eq(BaseGoodsData::getLocationCode, goodsData.getLocationCode());
        List<BaseGoodsData> baseGoodsData = baseGoodsDataMapper.selectList(queryWrapper);
        return baseGoodsData;
    }

    @Override
    public List<BaseGoodsData> getBaseGoodDataInByDerice(BaseDerive baseDerive){
        return baseGoodsDataMapper.getBaseGoodDataInByDerice(baseDerive);
    }
    @Override
    public List<BaseGoodsData> getBaseGoodDatarear(BaseDerive baseDerive){
        return baseGoodsDataMapper.getBaseGoodDatarear(baseDerive);
    }
}
