package com.ruoyi.basicData.service.impl;

import com.ruoyi.basicData.domain.TGoods;
import com.ruoyi.basicData.mapper.TGoodsMapper;
import com.ruoyi.basicData.service.ITGoodsService;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.warehouseBusiness.domain.TWarehouseBills;
import com.ruoyi.warehouseBusiness.domain.TWarehousebillsitems;
import com.ruoyi.warehouseBusiness.mapper.TWarehousebillsitemsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;
import java.util.Map;

/**
 * 商品详情Service业务层处理
 *
 * @author ruoyi
 * @date 2020-12-11
 */
@Service
public class TGoodsServiceImpl implements ITGoodsService {
    @Autowired
    private TGoodsMapper tGoodsMapper;

    @Autowired
    private TWarehousebillsitemsMapper tWarehousebillsitemsMapper;

    /**
     * 查询商品详情
     *
     * @param fId 商品详情ID
     * @return 商品详情
     */
    @Override
    public TGoods selectTGoodsById(Long fId) {
        return tGoodsMapper.selectTGoodsById(fId);
    }

    /**
     * 查询商品详情列表
     *
     * @param tGoods 商品详情
     * @return 商品详情
     */
    @Override
    public List<TGoods> selectTGoodsList(TGoods tGoods) {
        return tGoodsMapper.selectTGoodsList(tGoods);
    }

    /**
     * 新增商品详情
     *
     * @param tGoods 商品详情
     * @return 结果
     */
    @Override
    public int insertTGoods(TGoods tGoods) {
        tGoods.setCreateTime(DateUtils.getNowDate());
        return tGoodsMapper.insertTGoods(tGoods);
    }

    /**
     * 修改商品详情
     *
     * @param tGoods 商品详情
     * @return 结果
     */
    @Override
    public AjaxResult updateTGoods(TGoods tGoods) {
        if(tGoods.getfStatus()!=null && tGoods.getfStatus().equals("1")){
            TWarehousebillsitems tWarehousebillsitems =new TWarehousebillsitems();
            tWarehousebillsitems.setfGoodsid(tGoods.getfId());
            List<TWarehousebillsitems> warehousebillsitemsList1 =tWarehousebillsitemsMapper.selectTWarehousebillsitemsList(tWarehousebillsitems);
            if(warehousebillsitemsList1!=null && !warehousebillsitemsList1.isEmpty()){
                return AjaxResult.error("商品在库存总账有记录不可变更状态");
            }
        }
        tGoods.setUpdateTime(DateUtils.getNowDate());
        tGoodsMapper.updateTGoods(tGoods);
        return AjaxResult.success();
    }

    /**
     * 批量删除商品详情
     *
     * @param fIds 需要删除的商品详情ID
     * @return 结果
     */
    @Override
    @Transactional
    public AjaxResult deleteTGoodsByIds(Long[] fIds) {
        // return tGoodsMapper.deleteTGoodsByIds(fIds);
        int i = 1;
        for(Long id:fIds){
            TWarehousebillsitems tWarehousebillsitems =new TWarehousebillsitems();
            tWarehousebillsitems.setfGoodsid(id);
            List<TWarehousebillsitems> warehousebillsitemsList1 =tWarehousebillsitemsMapper.selectTWarehousebillsitemsList(tWarehousebillsitems);
            if(warehousebillsitemsList1!=null && !warehousebillsitemsList1.isEmpty()){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return AjaxResult.error("商品第" + i + "行 仓库有货物不可删除");
            }
            tGoodsMapper.deleteTGoodsById(id);
            i++;
        }
        return AjaxResult.success();
    }

    /**
     * 删除商品详情信息
     *
     * @param fId 商品详情ID
     * @return 结果
     */
    @Override
    public int deleteTGoodsById(Long fId) {
        return tGoodsMapper.deleteTGoodsById(fId);
    }

    @Override
    public String checkUFNoUnique(TGoods tGoods) {
        TGoods tGoods1 = tGoodsMapper.checkFNoUnique(tGoods.getfNo());
        if (StringUtils.isNotNull(tGoods1) && tGoods1.getfId()!=tGoods.getfId()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public String checkUFNnameUnique(TGoods tGoods) {
        TGoods tGoods1 = tGoodsMapper.checkUFNnameUnique(tGoods.getfName());
        if (StringUtils.isNotNull(tGoods1) && tGoods1.getfId()!=tGoods.getfId()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public List<Map<String, Object>> getGoodName() {
        return tGoodsMapper.getGoodName();
    }
}
