package com.ruoyi.basicData.service.impl;

import com.ruoyi.basicData.domain.TFees;
import com.ruoyi.basicData.domain.TGoods;
import com.ruoyi.basicData.mapper.TFeesMapper;
import com.ruoyi.basicData.service.ITFeesService;
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.TWarehousebillsfees;
import com.ruoyi.warehouseBusiness.domain.TWarehousebillsitems;
import com.ruoyi.warehouseBusiness.mapper.TWarehousebillsfeesMapper;
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;

/**
 * 费用信息Service业务层处理
 *
 * @author ruoyi
 * @date 2020-12-11
 */
@Service
public class TFeesServiceImpl implements ITFeesService {
    @Autowired
    private TFeesMapper tFeesMapper;

    @Autowired
    private TWarehousebillsfeesMapper tWarehousebillsfeesMapper;
    /**
     * 查询费用信息
     *
     * @param fId 费用信息ID
     * @return 费用信息
     */
    @Override
    public TFees selectTFeesById(Long fId) {
        return tFeesMapper.selectTFeesById(fId);
    }

    /**
     * 查询费用信息列表
     *
     * @param tFees 费用信息
     * @return 费用信息
     */
    @Override
    public List<TFees> selectTFeesList(TFees tFees) {
        return tFeesMapper.selectTFeesList(tFees);
    }

    /**
     * 新增费用信息
     *
     * @param tFees 费用信息
     * @return 结果
     */
    @Override
    public int insertTFees(TFees tFees) {
        tFees.setCreateTime(DateUtils.getNowDate());
        return tFeesMapper.insertTFees(tFees);
    }

    /**
     * 修改费用信息
     *
     * @param tFees 费用信息
     * @return 结果
     */
    @Override
    @Transactional
    public AjaxResult updateTFees(TFees tFees) {
        String modify = tFeesMapper.selectTFeesModify(tFees);
        if ("F".equals(modify)) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error("该费用不允许修改数据");
        }
        tFees.setUpdateTime(DateUtils.getNowDate());
        tFeesMapper.updateTFees(tFees);
        return AjaxResult.success();
    }

    /**
     * 批量删除费用信息
     *
     * @param fIds 需要删除的费用信息ID
     * @return 结果
     */
    @Override
    @Transactional
    public AjaxResult deleteTFeesByIds(Long[] fIds) {
        // return tFeesMapper.deleteTFeesByIds(fIds);
        int i = 1;
        for(Long id:fIds){
            TFees tFees = new TFees();
            tFees.setfId(id);
            String modify = tFeesMapper.selectTFeesModify(tFees);
            if ("F".equals(modify)) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return AjaxResult.error("费用第" + i + "行为不允许修改数据");
            }
            TWarehousebillsfees tWarehousebillsfees =new TWarehousebillsfees();
            tWarehousebillsfees.setfFeeid(id);
            List<TWarehousebillsfees> TWarehousebillsfeesList =tWarehousebillsfeesMapper.selectTWarehousebillsfeesList(tWarehousebillsfees);
            if(TWarehousebillsfeesList!=null && !TWarehousebillsfeesList.isEmpty()){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return AjaxResult.error("费用第" + i + "行不可删除");
            }
            tFeesMapper.deleteTFeesById(id);
            i++;
        }
        return AjaxResult.success();
    }

    /**
     * 删除费用信息信息
     *
     * @param fId 费用信息ID
     * @return 结果
     */
    @Override
    public int deleteTFeesById(Long fId) {
        return tFeesMapper.deleteTFeesById(fId);
    }

    @Override
    public String checkUFNoUnique(TFees tFees) {
        TFees tFees1= tFeesMapper.checkFNoUnique(tFees.getfNo());
        if (StringUtils.isNotNull(tFees1) && tFees1.getfId()!=tFees.getfId()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public String checkUFNnameUnique(TFees tFees) {
        TFees tFees1 = tFeesMapper.checkUFNnameUnique(tFees.getfName());
        if (StringUtils.isNotNull(tFees1) && tFees1.getfId()!=tFees.getfId()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }
}
