package com.ruoyi.project.business.service.quotation.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.project.business.domain.TqAdditionalFees;
import com.ruoyi.project.business.domain.TqSpecificationSurcharge;
import com.ruoyi.project.business.domain.bo.quotation.AdditionFeeAddBO;
import com.ruoyi.project.business.domain.dto.TqSpecificationSurchargeDto;
import com.ruoyi.project.business.enums.DelFlagEnum;
import com.ruoyi.project.business.mapper.quotation.TqAdditionalFeesMapper;
import com.ruoyi.project.business.service.quotation.ITqAdditionalFeesService;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2024-05-16
 */
@Service
public class TqAdditionalFeesServiceImpl extends ServiceImpl<TqAdditionalFeesMapper, TqAdditionalFees> implements
    ITqAdditionalFeesService {

    @Resource
    private TqAdditionalFeesMapper tqAdditionalFeesMapper;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public TqAdditionalFees selectTqAdditionalFeesById(Long id) {
        return tqAdditionalFeesMapper.selectTqAdditionalFeesById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param tqAdditionalFees 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<TqAdditionalFees> selectTqAdditionalFeesList(TqAdditionalFees tqAdditionalFees) {
        return tqAdditionalFeesMapper.selectTqAdditionalFeesList(tqAdditionalFees);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param tqAdditionalFees 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertTqAdditionalFees(TqAdditionalFees tqAdditionalFees) {
        tqAdditionalFees.setCreateTime(DateUtils.getNowDate());
        return tqAdditionalFeesMapper.insertTqAdditionalFees(tqAdditionalFees);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param tqAdditionalFees 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateTqAdditionalFees(TqAdditionalFees tqAdditionalFees) {
        tqAdditionalFees.setUpdateTime(DateUtils.getNowDate());
        return tqAdditionalFeesMapper.updateTqAdditionalFees(tqAdditionalFees);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteTqAdditionalFeesByIds(Long[] ids) {
        return tqAdditionalFeesMapper.deleteTqAdditionalFeesByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteTqAdditionalFeesById(Long id) {
        return tqAdditionalFeesMapper.deleteTqAdditionalFeesById(id);
    }

    @Override
    public List<TqAdditionalFees> listByQuotationId(Long quotationId) {
        List<TqAdditionalFees> tqAdditionalFees = baseMapper.selectList(
            Wrappers.<TqAdditionalFees>lambdaQuery()
                .eq(TqAdditionalFees::getQuotationId, quotationId)
                .eq(TqAdditionalFees::getDelFlag, DelFlagEnum.NORMAL.getValue()));
        return tqAdditionalFees;
    }

    @Override
    @Transactional
    public void batchAdd(AdditionFeeAddBO addBO) {
        LambdaUpdateWrapper<TqAdditionalFees> deleteWrapper = Wrappers.<TqAdditionalFees>lambdaUpdate()
            .eq(TqAdditionalFees::getQuotationId, addBO.getQuotationId())
            .set(TqAdditionalFees::getDelFlag, DelFlagEnum.DELETED.getValue());
        baseMapper.update(null, deleteWrapper);
        List<TqAdditionalFees> additionalFeesList = addBO.getAdditionalFeesList();
        if (CollUtil.isNotEmpty(additionalFeesList)) {
            additionalFeesList = additionalFeesList.stream().peek(item -> {
                item.setDelFlag(DelFlagEnum.NORMAL.getValue());
                item.setCreateBy(SecurityUtils.getStrUserId());
                item.setCreateTime(DateUtils.getNowDate());
                item.setUpdateBy(SecurityUtils.getStrUserId());
                item.setUpdateTime(DateUtils.getNowDate());
            }).collect(Collectors.toList());
            this.saveBatch(additionalFeesList);
        }
    }

    @Override
    public List<TqSpecificationSurchargeDto> listByQuotationIdNew(Long quotationId) {
        return tqAdditionalFeesMapper.listByQuotationIdNew(quotationId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchAddNew(List<TqSpecificationSurcharge> tqSpecificationSurcharges) {
        if (CollectionUtils.isEmpty(tqSpecificationSurcharges)){
            return;
        }
        // 根据规格id删除数据重新新增
        List<Long> specificationIds = tqSpecificationSurcharges.stream().map(TqSpecificationSurcharge::getSpecificationId).collect(Collectors.toList());
        tqAdditionalFeesMapper.deleteTqSpecificationSurcharge(specificationIds);
        tqAdditionalFeesMapper.insertTqSpecificationSurcharge(tqSpecificationSurcharges);
    }
}
