package com.njzscloud.supervisory.order.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
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.core.ex.Exceptions;
import com.njzscloud.common.mp.support.PageParam;
import com.njzscloud.common.mp.support.PageResult;
import com.njzscloud.supervisory.goods.pojo.entity.GoodsCategoryEntity;
import com.njzscloud.supervisory.goods.pojo.entity.GoodsInfoEntity;
import com.njzscloud.supervisory.goods.service.GoodsCategoryService;
import com.njzscloud.supervisory.goods.service.GoodsInfoService;
import com.njzscloud.supervisory.order.mapper.OrderGoodsMapper;
import com.njzscloud.supervisory.order.pojo.entity.OrderGoodsEntity;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * 订单产品表
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class OrderGoodsService extends ServiceImpl<OrderGoodsMapper, OrderGoodsEntity> implements IService<OrderGoodsEntity> {

    private final GoodsInfoService goodsInfoService;
    private final GoodsCategoryService goodsCategoryService;
    /**
     * 新增
     */
    @Transactional(rollbackFor = Exception.class)
    public long add(Long goodsId, BigDecimal unitPrice) {
        GoodsInfoEntity goodsInfoEntity = goodsInfoService.getById(goodsId);
        Assert.notNull(goodsInfoEntity, () -> Exceptions.clierr("产品不存在"));
        goodsInfoEntity.setId(null);
        OrderGoodsEntity orderGoodsEntity = BeanUtil.copyProperties(goodsInfoEntity, OrderGoodsEntity.class)
                .setOriginGoodsId(goodsId);
        if (null != unitPrice) {
            orderGoodsEntity.setUnitPrice(unitPrice);
        }
        this.save(orderGoodsEntity);
        return orderGoodsEntity.getId();
    }

    /**
     * 修改
     */
    public void modify(OrderGoodsEntity orderGoodsEntity) {
        Long originGoodsId = orderGoodsEntity.getOriginGoodsId();
        GoodsInfoEntity goodsInfoEntity = goodsInfoService.getById(originGoodsId);
        Assert.notNull(goodsInfoEntity, () -> Exceptions.clierr("产品不存在"));
        GoodsCategoryEntity goodsCategoryEntity = goodsCategoryService.getById(goodsInfoEntity.getGoodsCategoryId());
        Assert.notNull(goodsCategoryEntity, () -> Exceptions.clierr("产品分类不存在"));
        this.updateById(orderGoodsEntity
                .setGoodsName(goodsInfoEntity.getGoodsName())
                .setUnitPrice(goodsInfoEntity.getUnitPrice())
                .setUnit(goodsInfoEntity.getUnit())
                .setMoneyStrategy(goodsInfoEntity.getMoneyStrategy())
                .setMoneyConfigId(goodsInfoEntity.getMoneyConfigId())
                .setPicture(goodsInfoEntity.getPicture())
                .setTaxRate(goodsInfoEntity.getTaxRate())
        );
    }

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

    /**
     * 详情
     */
    public OrderGoodsEntity detail(Long id) {
        return this.getById(id);
    }

    /**
     * 分页查询
     */
    public PageResult<OrderGoodsEntity> paging(PageParam pageParam, OrderGoodsEntity orderGoodsEntity) {
        return PageResult.of(this.page(pageParam.toPage(), Wrappers.<OrderGoodsEntity>query(orderGoodsEntity)));
    }

}
