package com.njzscloud.supervisory.goods.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njzscloud.common.mp.support.PageParam;
import com.njzscloud.common.mp.support.PageResult;
import com.njzscloud.supervisory.goods.mapper.GoodsInfoMapper;
import com.njzscloud.supervisory.goods.pojo.entity.GoodsCategoryEntity;
import com.njzscloud.supervisory.goods.pojo.entity.GoodsInfoEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 产品信息
 */
@Slf4j
@Service
public class GoodsInfoService extends ServiceImpl<GoodsInfoMapper, GoodsInfoEntity> implements IService<GoodsInfoEntity> {

    @Autowired
    private GoodsCategoryService goodsCategoryService;

    /**
     * 新增
     */
    public void add(GoodsInfoEntity goodsInfoEntity) {
        this.save(goodsInfoEntity);
    }

    /**
     * 修改
     */
    public void modify(GoodsInfoEntity goodsInfoEntity) {
        this.updateById(goodsInfoEntity);
    }

    /**
     * 删除
     */
    @Transactional(rollbackFor = Exception.class)
    public void del(List<Long> ids) {
        this.removeBatchByIds(ids);
    }

    /**
     * 详情
     */
    public GoodsInfoEntity detail(Long id) {
        GoodsInfoEntity entity = this.getById(id);
        if (null != entity) {
            GoodsCategoryEntity categoryEntity = goodsCategoryService.detail(Long.valueOf(entity.getGoodsCategoryId()));
            if (null != categoryEntity) {
                entity.setGoodsCategoryName(categoryEntity.getCategoryName());
                entity.setInventoryType(categoryEntity.getInventoryType());
            }
        }
        return entity;
    }

    /**
     * 分页查询
     */
    public PageResult<GoodsInfoEntity> paging(PageParam pageParam, GoodsInfoEntity goodsInfoEntity) {
        String strategy = "";
        if (null != goodsInfoEntity.getMoneyStrategy()) {
            strategy = goodsInfoEntity.getMoneyStrategy().getVal();
        }
        IPage<GoodsInfoEntity> page = this.page(pageParam.toPage(), Wrappers.<GoodsInfoEntity>lambdaQuery()
                .like(StrUtil.isNotBlank(goodsInfoEntity.getGoodsName()), GoodsInfoEntity::getGoodsName,
                        goodsInfoEntity.getGoodsName())
                .eq(StrUtil.isNotBlank(strategy), GoodsInfoEntity::getMoneyStrategy, strategy)
                .eq(StrUtil.isNotBlank(goodsInfoEntity.getGoodsCategoryId()), GoodsInfoEntity::getGoodsCategoryId,
                        goodsInfoEntity.getGoodsCategoryId()));
        for (GoodsInfoEntity entity: page.getRecords()) {
            GoodsCategoryEntity categoryEntity = goodsCategoryService.getById(entity.getGoodsCategoryId());
            if (null != categoryEntity) {
                entity.setGoodsCategoryName(categoryEntity.getCategoryName());
                entity.setInventoryType(categoryEntity.getInventoryType());
            }
        }
        return PageResult.of(page);
    }

    /**
     * 设置是否确认磅重
     */
    @Transactional(rollbackFor = Exception.class)
    public void setPoundWeight(Long id, Boolean isPoundWeight) {
        GoodsInfoEntity entity = new GoodsInfoEntity();
        entity.setId(id);
        entity.setIsPoundWeight(isPoundWeight);
        this.updateById(entity);
    }

    /**
     * 设置是否有回执单
     */
    @Transactional(rollbackFor = Exception.class)
    public void setReceipt(Long id, Boolean isReceipt) {
        GoodsInfoEntity entity = new GoodsInfoEntity();
        entity.setId(id);
        entity.setIsReceipt(isReceipt);
        this.updateById(entity);
    }

    /**
     * 设置是否上报
     */
    @Transactional(rollbackFor = Exception.class)
    public void setReport(Long id, Boolean isReport) {
        GoodsInfoEntity entity = new GoodsInfoEntity();
        entity.setId(id);
        entity.setIsReport(isReport);
        this.updateById(entity);
    }

    /**
     * 设置是否预约
     */
    @Transactional(rollbackFor = Exception.class)
    public void setSubscribe(Long id, Boolean isSubscribe) {
        GoodsInfoEntity entity = new GoodsInfoEntity();
        entity.setId(id);
        entity.setIsSubscribe(isSubscribe);
        this.updateById(entity);
    }

}
