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

import cn.hutool.core.bean.BeanUtil;
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.SpecialCharacterEscaper;
import com.ruoyi.project.business.domain.TqPriceDetail;
import com.ruoyi.project.business.domain.TqQuotation;
import com.ruoyi.project.business.domain.TqSpecification;
import com.ruoyi.project.business.domain.TqSpecificationNonStandard;
import com.ruoyi.project.business.domain.bo.quotation.NonstandardStatisticsQueryBO;
import com.ruoyi.project.business.domain.bo.quotation.PriceDetailQueryBO;
import com.ruoyi.project.business.domain.vo.quotation.DescFileVO;
import com.ruoyi.project.business.domain.vo.quotation.NonstandardStatisticsVO;
import com.ruoyi.project.business.domain.vo.quotation.PriceDetailVO;
import com.ruoyi.project.business.enums.DelFlagEnum;
import com.ruoyi.project.business.mapper.quotation.TqPriceDetailMapper;
import com.ruoyi.project.business.mapper.quotation.TqSpecificationNonStandardMapper;
import com.ruoyi.project.business.service.ITbFileService;
import com.ruoyi.project.business.service.quotation.ITqPriceDetailService;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.Resource;

import com.ruoyi.project.business.service.quotation.ITqQuotationService;
import com.ruoyi.project.business.service.quotation.ITqSpecificationService;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2024-05-21
 */
@Service
public class TqPriceDetailServiceImpl extends ServiceImpl<TqPriceDetailMapper, TqPriceDetail> implements
    ITqPriceDetailService {

    @Resource
    private TqPriceDetailMapper tqPriceDetailMapper;

    @Resource
    private TqSpecificationNonStandardMapper tqSpecificationNonStandardMapper;

    @Resource
    private ITbFileService tbFileService;

    @Resource
    @Lazy
    private ITqSpecificationService tqSpecificationService;

    @Resource
    @Lazy
    private ITqQuotationService tqQuotationService;
    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public TqPriceDetail selectTqPriceDetailById(Long id) {
        return tqPriceDetailMapper.selectTqPriceDetailById(id);
    }

    @Override
    public List<PriceDetailVO> listAll(PriceDetailQueryBO queryBO) {
        List<PriceDetailVO> priceDetailVOS = tqPriceDetailMapper.listAll(queryBO);
        List<TqSpecificationNonStandard> tqSpecificationNonStandards = tqSpecificationNonStandardMapper.selectList(
            Wrappers.<TqSpecificationNonStandard>lambdaQuery()
                .eq(TqSpecificationNonStandard::getSpecificationId, queryBO.getSpecificationId())
                .eq(TqSpecificationNonStandard::getDelFlag, DelFlagEnum.NORMAL.getValue())
        );
        List<Long> nonStandardDescIdList = tqSpecificationNonStandards.stream()
            .map(TqSpecificationNonStandard::getId)
            .collect(Collectors.toList());
        // 获取非标描述文件
        Map<Long, List<DescFileVO>> descFileVOMap = tbFileService.getDescFileVOMap(nonStandardDescIdList);
        List<PriceDetailVO> nonStandardDescVo = tqSpecificationNonStandards.stream().map(item -> {
            PriceDetailVO priceDetailVO = new PriceDetailVO();
            BeanUtil.copyProperties(item, priceDetailVO);
            priceDetailVO.setMarkupName(item.getNonStandardDesc());
            priceDetailVO.setNonStandardDescFlag(true);
            priceDetailVO.setFileList(descFileVOMap.getOrDefault(item.getId(), new ArrayList<>()));
            return priceDetailVO;
        }).collect(Collectors.toList());
        priceDetailVOS.addAll(nonStandardDescVo);

        TqSpecification tqSpecification = tqSpecificationService.getById(queryBO.getSpecificationId());
        TqQuotation tqQuotation = tqQuotationService.getById(tqSpecification.getQuotationId());
        priceDetailVOS.forEach(item -> {
            item.setDiscountRate(tqSpecification.getBasicPrice().divide(tqSpecification.getOriginalBasicPrice(), 6, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)));
            item.setDiscountFlag(tqQuotation.getDiscountFlag());
        });
        return priceDetailVOS;
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param tqPriceDetail 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<TqPriceDetail> selectTqPriceDetailList(TqPriceDetail tqPriceDetail) {
        return tqPriceDetailMapper.selectTqPriceDetailList(tqPriceDetail);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param tqPriceDetail 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertTqPriceDetail(TqPriceDetail tqPriceDetail) {
        tqPriceDetail.setCreateTime(DateUtils.getNowDate());
        return tqPriceDetailMapper.insertTqPriceDetail(tqPriceDetail);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param tqPriceDetail 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateTqPriceDetail(TqPriceDetail tqPriceDetail) {
        tqPriceDetail.setUpdateTime(DateUtils.getNowDate());
        return tqPriceDetailMapper.updateTqPriceDetail(tqPriceDetail);
    }

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

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

    @Override
    public List<NonstandardStatisticsVO> listNonstandardStatistics(NonstandardStatisticsQueryBO queryBO) {
        SpecialCharacterEscaper.escapeSpecialCharacters(queryBO
            , NonstandardStatisticsQueryBO::getProjectNo
            , NonstandardStatisticsQueryBO::getProjectName
            , NonstandardStatisticsQueryBO::getQuotationNo
            , NonstandardStatisticsQueryBO::getMarkupName
            , NonstandardStatisticsQueryBO::getRelatedParaInfo
            , NonstandardStatisticsQueryBO::getSolution
            , NonstandardStatisticsQueryBO::getSolutionMachine
            , NonstandardStatisticsQueryBO::getNonStandardReason
        );
        return tqPriceDetailMapper.listNonstandardStatistics(queryBO);
    }
}
