package com.ruoyi.buyerapi.service.impl;

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

import cn.hutool.core.util.IdUtil;
import com.ruoyi.buyerapi.domain.BuyerFileInfo;
import com.ruoyi.buyerapi.mapper.BuyerFileInfoMapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.buyerapi.mapper.BuyerGoodsMapper;
import com.ruoyi.buyerapi.domain.BuyerGoods;
import com.ruoyi.buyerapi.service.IBuyerGoodsService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 商品管理Service业务层处理
 *
 * @author ruoyi
 * @date 2022-09-19
 */
@Service
public class BuyerGoodsServiceImpl implements IBuyerGoodsService {
    @Autowired
    private BuyerGoodsMapper buyerGoodsMapper;

    @Autowired
    private BuyerFileInfoMapper buyerFileInfoMapper;

    /**
     * 查询商品管理
     *
     * @param id 商品管理主键
     * @return 商品管理
     */
    @Transactional
    @Override
    public BuyerGoods selectBuyerGoodsById(String id) {
        BuyerGoods buyerGoods = buyerGoodsMapper.selectBuyerGoodsById(id);
        // 查询关联文件
        List<BuyerFileInfo> buyerFileInfos = buyerFileInfoMapper.selectBuyerFileInfoByLinkId(id);
        buyerGoods.setBuyerFileInfos(buyerFileInfos);
        return buyerGoods;
    }

    /**
     * 查询商品管理列表
     *
     * @param buyerGoods 商品管理
     * @return 商品管理
     */
    @Transactional
    @Override
    public List<BuyerGoods> selectBuyerGoodsList(BuyerGoods buyerGoods) {
        List<BuyerGoods> buyerGoodss = buyerGoodsMapper.selectBuyerGoodsList(buyerGoods);
        // 查询关联文件
        for (BuyerGoods goods : buyerGoodss) {
            List<BuyerFileInfo> buyerFileInfos = buyerFileInfoMapper.selectBuyerFileInfoByLinkId(goods.getId());
            goods.setBuyerFileInfos(buyerFileInfos);
        }
        return buyerGoodss;
    }

    /**
     * 查询商品销售额
     *
     * @return 品销售额
     */
    @Override
    public BigDecimal selectMoneyBuyerGoods() {
        return buyerGoodsMapper.selectMoneyBuyerGoods();
    }

    /**
     * 新增商品管理
     *
     * @param buyerGoods 商品管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBuyerGoods(BuyerGoods buyerGoods) {
        buyerGoods.setCreateTime(DateUtils.getNowDate());
        // 新增关联文件
        List<BuyerFileInfo> buyerFileInfos = buyerGoods.getBuyerFileInfos();
        if (buyerFileInfos != null && buyerFileInfos.size()>0) {
            for (BuyerFileInfo buyerFileInfo : buyerFileInfos) {
                buyerFileInfo.setId(IdUtil.simpleUUID());
                buyerFileInfo.setCreateTime(DateUtils.getNowDate());
                buyerFileInfo.setCreateBy(SecurityUtils.getUsername());
                buyerFileInfo.setLinkId(buyerGoods.getId());
            }
            buyerFileInfoMapper.batchInsertBuyerFileInfo(buyerFileInfos);
        }
        return buyerGoodsMapper.insertBuyerGoods(buyerGoods);
    }

    /**
     * 修改商品管理
     *
     * @param buyerGoods 商品管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateBuyerGoods(BuyerGoods buyerGoods) {
        buyerGoods.setUpdateTime(DateUtils.getNowDate());
        // 修改关联文件
        List<BuyerFileInfo> buyerFileInfos = buyerGoods.getBuyerFileInfos();
        if (buyerFileInfos != null && buyerFileInfos.size()>0) {
            for (BuyerFileInfo buyerFileInfo : buyerFileInfos) {
                buyerFileInfo.setId(IdUtil.simpleUUID());
                buyerFileInfo.setUpdateTime(DateUtils.getNowDate());
                buyerFileInfo.setUpdateBy(SecurityUtils.getUsername());
                buyerFileInfo.setLinkId(buyerGoods.getId());
            }
            buyerFileInfoMapper.deleteBuyerFileInfoByLinkId(buyerGoods.getId());
            buyerFileInfoMapper.batchInsertBuyerFileInfo(buyerFileInfos);
        }else if (buyerFileInfos != null && buyerFileInfos.size() == 0) {
            buyerFileInfoMapper.deleteBuyerFileInfoByLinkId(buyerGoods.getId());
        }
        return buyerGoodsMapper.updateBuyerGoods(buyerGoods);
    }

    /**
     * 批量删除商品管理
     *
     * @param ids 需要删除的商品管理主键
     * @return 结果
     */
    @Override
    public int deleteBuyerGoodsByIds(String[] ids) {
        return buyerGoodsMapper.deleteBuyerGoodsByIds(ids);
    }

    /**
     * 删除商品管理信息
     *
     * @param id 商品管理主键
     * @return 结果
     */
    @Override
    public int deleteBuyerGoodsById(String id) {
        return buyerGoodsMapper.deleteBuyerGoodsById(id);
    }
}
