package com.njzscloud.dispose.goods.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
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.common.sn.support.SnUtil;
import com.njzscloud.dispose.finance.constant.ExpenseItemCategory;
import com.njzscloud.dispose.finance.pojo.entity.ExpenseItemEntity;
import com.njzscloud.dispose.finance.pojo.param.AddExpenseItemParam;
import com.njzscloud.dispose.finance.pojo.param.ModifyExpenseItemParam;
import com.njzscloud.dispose.finance.service.ExpenseItemService;
import com.njzscloud.dispose.goods.contant.BizType;
import com.njzscloud.dispose.goods.mapper.GoodsMapper;
import com.njzscloud.dispose.goods.pojo.entity.GoodsEntity;
import com.njzscloud.dispose.goods.pojo.param.AddGoodsParam;
import com.njzscloud.dispose.goods.pojo.param.ModifyGoodsParam;
import com.njzscloud.dispose.goods.pojo.param.SearchGoodsParam;
import com.njzscloud.dispose.goods.pojo.result.SearchGoodsResult;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 产品
 *
 * @author ljw
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class GoodsService extends ServiceImpl<GoodsMapper, GoodsEntity> {

    private final ExpenseItemService expenseItemService;

    /**
     * 新增
     */
    @Transactional(rollbackFor = Exception.class)
    public void add(AddGoodsParam addGoodsParam) {
        GoodsEntity goodsEntity = BeanUtil.copyProperties(addGoodsParam, GoodsEntity.class)
            .setSn(SnUtil.next("Goods-SN"));
        this.save(goodsEntity);
        // 付费项
        AddGoodsParam.ExpenseItem expenseItem = addGoodsParam.getExpenseItem();
        if (expenseItem == null) {
            return;
        }
        String goodsName = addGoodsParam.getGoodsName();
        AddExpenseItemParam addExpenseItemParam = BeanUtil.copyProperties(expenseItem, AddExpenseItemParam.class)
            .setGoodsId(goodsEntity.getId())
            .setExpenseItemName(goodsName)
            .setExpenseItemCategory(ExpenseItemCategory.ChanPin)
            .setMemo(goodsName + "费用配置");
        expenseItemService.add(addExpenseItemParam);
    }

    /**
     * 修改
     */
    @Transactional(rollbackFor = Exception.class)
    public void modify(ModifyGoodsParam modifyGoodsParam) {
        GoodsEntity goodsEntity = BeanUtil.copyProperties(modifyGoodsParam, GoodsEntity.class).setSn(null);
        this.updateById(goodsEntity);
        // 付费项
        ModifyGoodsParam.ExpenseItem expenseItem = modifyGoodsParam.getExpenseItem();

        String goodsName = modifyGoodsParam.getGoodsName();
        ModifyExpenseItemParam modifyExpenseItemParam = BeanUtil.copyProperties(expenseItem, ModifyExpenseItemParam.class)
            .setExpenseItemName(goodsName)
            .setMemo(goodsName + "费用配置");
        expenseItemService.modify(modifyExpenseItemParam);
    }

    /**
     * 删除
     */
    @Transactional(rollbackFor = Exception.class)
    public void del(List<Long> ids) {
        // 删除产品
        this.removeBatchByIds(ids);
        // 删除对应的费用配置
        expenseItemService.remove(Wrappers.<ExpenseItemEntity>lambdaQuery()
            .in(ExpenseItemEntity::getGoodsId, ids));
    }

    /**
     * 详情
     */
    public SearchGoodsResult detail(Long id) {
        return baseMapper.detail(Wrappers.query().eq("a.id", id));
    }

    /**
     * 分页查询
     */
    public PageResult<SearchGoodsResult> paging(PageParam pageParam, SearchGoodsParam goodsParam) {
        String keywords = goodsParam.getKeywords();
        BizType bizType = goodsParam.getBizType();
        Long goodsCategoryId = goodsParam.getGoodsCategoryId();
        String sn = goodsParam.getSn();
        String goodsName = goodsParam.getGoodsName();
        String specParams = goodsParam.getSpecParams();
        Boolean canuse = goodsParam.getCanuse();
        return PageResult.of(baseMapper.paging(pageParam.toPage(), Wrappers.query()
            .eq("a.deleted", Boolean.FALSE)
            .eq(bizType != null, "b.biz_type", bizType)
            .eq(goodsCategoryId != null, "a.goods_category_id", goodsCategoryId)
            .like(StrUtil.isNotBlank(sn), "a.sn", sn)
            .like(StrUtil.isNotBlank(goodsName), "a.goods_name", goodsName)
            .like(StrUtil.isNotBlank(specParams), "a.spec_params", specParams)
            .eq(canuse != null, "a.canuse", canuse)
            .and(StrUtil.isNotBlank(keywords), it -> it.like("a.sn", keywords)
                .or().like("a.goods_name", keywords)
            )
        ));
    }

    /**
     * 启用/禁用
     */
    public void enable(Long id, Boolean enable) {
        this.updateById(new GoodsEntity()
            .setId(id)
            .setCanuse(enable)
        );
    }
}
