package com.ruoyi.shipping.service.impl;

import java.util.List;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.shipping.domain.TShipDeal;
import com.ruoyi.shipping.domain.TShipDealItem;
import com.ruoyi.shipping.mapper.TShipDealItemMapper;
import com.ruoyi.shipping.mapper.TShipDealMapper;
import com.ruoyi.shipping.service.ITShipDealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 凯和费用协议Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-01-07
 */
@Service
public class TShipDealServiceImpl implements ITShipDealService
{
    @Resource
    private TShipDealMapper tShipDealMapper;
    @Resource
    private TShipDealItemMapper tShipDealItemMapper;

    /**
     * 查询凯和费用协议
     * 
     * @param fId 凯和费用协议ID
     * @return 凯和费用协议
     */
    @Override
    public TShipDeal selectTShipDealById(Long fId)
    {
        TShipDeal shipDeal = tShipDealMapper.selectTShipDealById(fId);
        if (shipDeal != null) {
            TShipDealItem tShipDealItem = new TShipDealItem();
            tShipDealItem.setfPid(shipDeal.getfId());
            shipDeal.settShipDealItemList(tShipDealItemMapper.selectTShipDealItemList(tShipDealItem));
        }
        return shipDeal;
    }

    /**
     * 查询凯和费用协议列表
     * 
     * @param tShipDeal 凯和费用协议
     * @return 凯和费用协议
     */
    @Override
    public List<TShipDeal> selectTShipDealList(TShipDeal tShipDeal)
    {
        return tShipDealMapper.selectTShipDealList(tShipDeal);
    }

    /**
     * 新增凯和费用协议
     * 
     * @param tShipDeal 凯和费用协议
     * @return 结果
     */
    @Override
    @Transactional
    public TShipDeal insertTShipDeal(TShipDeal tShipDeal)
    {
        if (tShipDeal.getfId() == null){
            tShipDeal.setCreateBy(SecurityUtils.getUsername());
            tShipDeal.setCreateTime(DateUtils.getNowDate());
            tShipDealMapper.insertTShipDeal(tShipDeal);
        }else {
            tShipDeal.setUpdateBy(SecurityUtils.getUsername());
            tShipDeal.setUpdateTime(DateUtils.getNowDate());
            tShipDealMapper.updateTShipDeal(tShipDeal);
        }
       if (CollectionUtils.isNotEmpty(tShipDeal.gettShipDealItemList())){
           tShipDeal.gettShipDealItemList().stream().forEach(item ->{
               if (item.getfId() == null){
                   item.setfPid(tShipDeal.getfId());
                   item.setCreateBy(SecurityUtils.getUsername());
                   item.setCreateTime(DateUtils.getNowDate());
                   tShipDealItemMapper.insertTShipDealItem(item);
               }else {
                   item.setfPid(tShipDeal.getfId());
                   item.setUpdateBy(SecurityUtils.getUsername());
                   item.setUpdateTime(DateUtils.getNowDate());
                   tShipDealItemMapper.updateTShipDealItem(item);
               }
           });
       }
        return tShipDeal;
    }

    /**
     * 修改凯和费用协议
     * 
     * @param tShipDeal 凯和费用协议
     * @return 结果
     */
    @Override
    public int updateTShipDeal(TShipDeal tShipDeal)
    {
        tShipDeal.setUpdateTime(DateUtils.getNowDate());
        return tShipDealMapper.updateTShipDeal(tShipDeal);
    }

    /**
     * 批量删除凯和费用协议
     * 
     * @param fIds 需要删除的凯和费用协议ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteTShipDealByIds(Long[] fIds)
    {
        int i = tShipDealMapper.deleteTShipDealByIds(fIds);
        tShipDealItemMapper.deleteTShipDealItemByPIds(fIds);
        return i;
    }

    /**
     * 删除凯和费用协议信息
     * 
     * @param fId 凯和费用协议ID
     * @return 结果
     */
    @Override
    public int deleteTShipDealById(Long fId)
    {
        return tShipDealMapper.deleteTShipDealById(fId);
    }
}
