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

import static com.ruoyi.project.business.enums.AuditFlowEnum.BARGAIN;
import static com.ruoyi.project.business.service.quotation.impl.TqQuotationServiceImpl.ONE_HUNDRED;

import cn.hutool.core.collection.CollUtil;
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.common.utils.StringUtils;
import com.ruoyi.project.business.domain.TqBargainPrice;
import com.ruoyi.project.business.domain.TqQuotation;
import com.ruoyi.project.business.domain.TqSalespersonSetPrice;
import com.ruoyi.project.business.domain.TqSpecification;
import com.ruoyi.project.business.domain.bo.quotation.BargainPriceAddBO;
import com.ruoyi.project.business.domain.bo.quotation.SpecificationQueryBO;
import com.ruoyi.project.business.dto.TaskGenerateParams;
import com.ruoyi.project.business.enums.BargainStatusEnum;
import com.ruoyi.project.business.enums.DelFlagEnum;
import com.ruoyi.project.business.enums.QuotationStatusEnum;
import com.ruoyi.project.business.mapper.quotation.TqBargainPriceMapper;
import com.ruoyi.project.business.mapper.quotation.TqQuotationMapper;
import com.ruoyi.project.business.mapper.quotation.TqSalespersonSetPriceMapper;
import com.ruoyi.project.business.mapper.quotation.TqSpecificationMapper;
import com.ruoyi.project.business.service.flow.IFlowService;
import com.ruoyi.project.business.service.quotation.ITqBargainPriceService;
import com.ruoyi.project.business.service.quotation.ITqSalespersonSetPriceService;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

    public static final String DISCOUNT_RATE = "discountRate";

    @Resource
    private TqBargainPriceMapper tqBargainPriceMapper;

    @Resource
    private TqSpecificationMapper tqSpecificationMapper;

    @Resource
    private TqQuotationMapper tqQuotationMapper;

    @Resource
    private TqSalespersonSetPriceMapper tqSalespersonSetPriceMapper;

    @Resource
    private IFlowService flowService;

    @Resource
    private ITqSalespersonSetPriceService tqSalespersonSetPriceService;

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

    /**
     * 查询【请填写功能名称】列表
     *
     * @param tqBargainPrice 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<TqBargainPrice> selectTqBargainPriceList(TqBargainPrice tqBargainPrice) {
        return tqBargainPriceMapper.selectTqBargainPriceList(tqBargainPrice);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param tqBargainPrice 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertTqBargainPrice(TqBargainPrice tqBargainPrice) {
        tqBargainPrice.setCreateTime(DateUtils.getNowDate());
        return tqBargainPriceMapper.insertTqBargainPrice(tqBargainPrice);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param tqBargainPrice 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateTqBargainPrice(TqBargainPrice tqBargainPrice) {
        tqBargainPrice.setUpdateTime(DateUtils.getNowDate());
        return tqBargainPriceMapper.updateTqBargainPrice(tqBargainPrice);
    }

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

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

    @Override
    @Transactional
    public void startBargainPrice(BargainPriceAddBO bargainPriceAddBO) {
        TqBargainPrice tqBargainPrice = new TqBargainPrice();
        tqBargainPrice.setQuotationId(bargainPriceAddBO.getQuotationId());
        tqBargainPrice.setBargainPrice(bargainPriceAddBO.getExpectedPrice());
        tqBargainPrice.setDelFlag(DelFlagEnum.NORMAL.getValue());
        tqBargainPrice.setCreateBy(SecurityUtils.getStrUserId());
        tqBargainPrice.setCreateTime(DateUtils.getNowDate());
        tqBargainPrice.setUpdateBy(SecurityUtils.getStrUserId());
        tqBargainPrice.setUpdateTime(DateUtils.getNowDate());
        this.save(tqBargainPrice);
        Long bargainPriceId = tqBargainPrice.getId();
        TqQuotation tqQuotation = tqQuotationMapper.selectById(bargainPriceAddBO.getQuotationId());
        // 计算报价的价格  基础价格  选配价格
        List<TqSpecification> tqSpecifications = tqSpecificationMapper.selectList(
            Wrappers.<TqSpecification>lambdaQuery()
                .eq(TqSpecification::getQuotationId, bargainPriceAddBO.getQuotationId())
                .eq(TqSpecification::getDelFlag, DelFlagEnum.NORMAL.getValue()));

        // 希望基价总和
        BigDecimal expectedBasicPrice = bargainPriceAddBO.getExpectedPrice().subtract(tqQuotation.getOptionalPrice());

        for (TqSpecification specification : tqSpecifications) {
            // 当前规格基价对总基价的占比
            BigDecimal divide = (specification.getBasicPrice()
                .multiply(BigDecimal.valueOf(specification.getElevatorNumber()))
            ).divide(tqQuotation.getBasicPrice(), 2, RoundingMode.HALF_UP);
            BigDecimal discountBasicPrice;
            BigDecimal discountRate;
            BigDecimal discountPrice;
            if (expectedBasicPrice.compareTo(BigDecimal.ZERO) <= 0) {
                // 折后的基价
                discountBasicPrice = BigDecimal.ZERO;
                // 折扣率
                discountRate = BigDecimal.ZERO;
                // 折后总价
                discountPrice = discountBasicPrice.add(specification.getOptionalPrice());
            } else {
                // 折后的基价总和
                BigDecimal discountBasicPriceTotal = expectedBasicPrice.multiply(divide)
                    .setScale(2, RoundingMode.HALF_UP);
                // 折后的基价
                discountBasicPrice = discountBasicPriceTotal.divide(
                    BigDecimal.valueOf(specification.getElevatorNumber()), 2, RoundingMode.HALF_UP);
                // 折扣率
                discountRate = discountBasicPrice.divide(specification.getBasicPrice(), 4, RoundingMode.HALF_UP)
                    .multiply(new BigDecimal(ONE_HUNDRED));
                // 折后总价
                discountPrice = discountBasicPrice.add(specification.getOptionalPrice());
            }
            // 新增议价定价表
            TqSalespersonSetPrice tqSalespersonSetPrice = new TqSalespersonSetPrice();
            tqSalespersonSetPrice.setBargainId(bargainPriceId);
            tqSalespersonSetPrice.setSpecificationId(specification.getId());
            tqSalespersonSetPrice.setBasicPrice(specification.getBasicPrice());
            tqSalespersonSetPrice.setOptionalPrice(specification.getOptionalPrice());
            tqSalespersonSetPrice.setDiscountRate(discountRate);
            tqSalespersonSetPrice.setDiscountbasicPrice(discountBasicPrice);
            tqSalespersonSetPrice.setDiscountPrice(discountPrice);
            tqSalespersonSetPrice.setDelFlag(DelFlagEnum.NORMAL.getValue());
            tqSalespersonSetPrice.setCreateBy(SecurityUtils.getStrUserId());
            tqSalespersonSetPrice.setCreateTime(DateUtils.getNowDate());
            tqSalespersonSetPrice.setUpdateBy(SecurityUtils.getStrUserId());
            tqSalespersonSetPrice.setUpdateTime(DateUtils.getNowDate());
            tqSalespersonSetPriceService.save(tqSalespersonSetPrice);
        }

        // 更新报价
        tqQuotationMapper.update(null, Wrappers.<TqQuotation>lambdaUpdate()
            .eq(TqQuotation::getId, bargainPriceAddBO.getQuotationId())
            .set(TqQuotation::getQuotationStatus, QuotationStatusEnum.BARGAIN_APPROVAL.getCode())
        );
        // 更新电梯规格
        tqSpecificationMapper.update(null, Wrappers.<TqSpecification>lambdaUpdate()
            .eq(TqSpecification::getQuotationId, bargainPriceAddBO.getQuotationId())
            .set(TqSpecification::getQuotationStatus, QuotationStatusEnum.BARGAIN_APPROVAL.getCode())
        );
        // 启动议价流程
        flowService.generateTasks(
            new TaskGenerateParams(BARGAIN, tqQuotation, SecurityUtils.getUserId(),
                tqQuotation.getQuotationNo()));
    }

    @Override
    @Transactional
    public void acceptBargainPrice(Long quotationId) {
        List<TqBargainPrice> list = this.list(
            Wrappers.<TqBargainPrice>lambdaQuery().eq(TqBargainPrice::getQuotationId, quotationId)
                .orderByDesc(TqBargainPrice::getId));
        if (CollUtil.isNotEmpty(list)) {
            // 获取最新议价
            TqBargainPrice tqBargainPrice = list.get(0);
            SpecificationQueryBO specificationQueryBO = new SpecificationQueryBO();
            specificationQueryBO.setBargainId(tqBargainPrice.getId());
            specificationQueryBO.setQuotationId(quotationId);
            // 获取全部规格
            List<TqSpecification> tqSpecifications = tqSpecificationMapper.selectList(
                Wrappers.<TqSpecification>lambdaQuery()
                    .eq(TqSpecification::getQuotationId, quotationId)
                    .eq(TqSpecification::getDelFlag, DelFlagEnum.NORMAL.getValue())
            );
            // 电梯型号id 数量Map   key:elevatorId  value:elevatorNumber
            Map<Long, Long> elevatorIdNumberMap = tqSpecifications.stream()
                .collect(Collectors.toMap(TqSpecification::getId, TqSpecification::getElevatorNumber));
            List<Long> specificationIdList = tqSpecifications.stream().map(TqSpecification::getId)
                .collect(Collectors.toList());
            // 获取议价价格明细
            List<TqSalespersonSetPrice> tqSalespersonSetPrices = tqSalespersonSetPriceMapper
                .selectList(Wrappers.<TqSalespersonSetPrice>lambdaQuery()
                    .eq(TqSalespersonSetPrice::getBargainId, tqBargainPrice.getId())
                    .eq(TqSalespersonSetPrice::getDelFlag, DelFlagEnum.NORMAL.getValue())
                    .in(TqSalespersonSetPrice::getSpecificationId, specificationIdList)
                    .orderByDesc(TqSalespersonSetPrice::getId)
                );
            // 获取最新的议价价格明细
            List<TqSalespersonSetPrice> newSalesSetPricesList = new ArrayList<>();
            Map<String, List<TqSalespersonSetPrice>> map = tqSalespersonSetPrices.stream()
                .collect(
                    Collectors.groupingBy(price -> StringUtils.join(price.getBargainId(), price.getSpecificationId())));
            map.forEach((key, value) -> newSalesSetPricesList.add(value.get(0)));
            // 统计接受价格
            BigDecimal acceptPrice = BigDecimal.ZERO;
            for (TqSalespersonSetPrice item : newSalesSetPricesList) {
                BigDecimal discountPrice = item.getDiscountPrice();
                // 更新最终价格
                tqSpecificationMapper.update(null,
                    Wrappers.<TqSpecification>lambdaUpdate()
                        .eq(TqSpecification::getId, item.getSpecificationId())
                        .set(TqSpecification::getFinalPrice, discountPrice)
                        .set(TqSpecification::getQuotationStatus, QuotationStatusEnum.ACCEPT_PRICE.getCode())
                );
                Long elevatorNumber = elevatorIdNumberMap.get(item.getSpecificationId());
                acceptPrice = acceptPrice.add(new BigDecimal(elevatorNumber).multiply(discountPrice));
            }
            // 更新议价
            this.update(Wrappers.<TqBargainPrice>lambdaUpdate()
                .eq(TqBargainPrice::getId, tqBargainPrice.getId())
                .set(TqBargainPrice::getBargainStatus, BargainStatusEnum.ACCEPT.getCode())
                .set(TqBargainPrice::getAcceptPrice, acceptPrice)
            );
            // 更新报价
            tqQuotationMapper.update(null, Wrappers.<TqQuotation>lambdaUpdate()
                .eq(TqQuotation::getId, quotationId)
                .set(TqQuotation::getQuotationStatus, QuotationStatusEnum.ACCEPT_PRICE.getCode())
                .set(TqQuotation::getFinalPrice, acceptPrice));
        }
    }

    @Override
    @Transactional
    public void rejectBargainPrice(Long quotationId) {
        // List<TqBargainPrice> list = this.list(
        //     Wrappers.<TqBargainPrice>lambdaQuery().eq(TqBargainPrice::getQuotationId, quotationId)
        //         .orderByDesc(TqBargainPrice::getId));
        // if (CollUtil.isNotEmpty(list)) {
        // 更新报价状态
        tqQuotationMapper.update(null, Wrappers.<TqQuotation>lambdaUpdate()
            .set(TqQuotation::getQuotationStatus, QuotationStatusEnum.BARGAIN_REJECT.getCode())
            .eq(TqQuotation::getId, quotationId));
        // }
    }
}
