package com.fjhx.sale.flow;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import com.fjhx.file.entity.ObsFile;
import com.fjhx.file.utils.ObsFileUtil;
import com.fjhx.flow.core.FlowDelegate;
import com.fjhx.sale.entity.quotation.dto.QuotationProductDto;
import com.fjhx.sale.entity.quotation.po.QuotationProduct;
import com.fjhx.sale.entity.sale.dto.SaleQuotationDto;
import com.fjhx.sale.entity.sale.po.SaleQuotation;
import com.fjhx.sale.service.quotation.QuotationProductService;
import com.fjhx.sale.service.sale.SaleQuotationService;
import com.ruoyi.common.core.domain.BasePo;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Component
public class DiscussPriceFlow extends FlowDelegate {

    @Autowired
    private SaleQuotationService saleQuotationService;
    @Autowired
    private QuotationProductService quotationProductService;

    @Override
    public String getFlowKey() {
        return "discuss_price_flow";
    }

    @Override
    public Long start(Long flowId, JSONObject submitData) {
        SaleQuotationDto saleQuotationDto = submitData.toJavaObject(SaleQuotationDto.class);
        List<QuotationProductDto> quotationProductList = saleQuotationDto.getQuotationProductList();
        for (QuotationProductDto quotationProductDto : quotationProductList) {
            //复制议价格信息
            quotationProductDto.setDiscussPrice(quotationProductDto.getPrice());
            quotationProductDto.setPrice(null);
        }

        //议价附件
        List<ObsFile> discussFileList = saleQuotationDto.getDiscussFileList();
        ObsFileUtil.editFile(discussFileList, saleQuotationDto.getId(), 10);

        saleQuotationService.updateById(saleQuotationDto);
        quotationProductService.updateBatchById(BeanUtil.copyToList(quotationProductList, QuotationProduct.class));

        //将code赋值给流程引擎
        SaleQuotation byId = saleQuotationService.getById(saleQuotationDto.getId());
        submitData.put("code", byId.getCode());

        return saleQuotationDto.getId();
    }

    @Override
    public void end(Long flowId, Long businessId, JSONObject submitData) {
        quotationProductService.update(q -> q
                .eq(QuotationProduct::getSaleQuotationId, businessId)
                .setSql("price = discuss_price")
                .set(BasePo::getUpdateUser, SecurityUtils.getUserId())
                .set(BasePo::getUpdateTime, new Date())
        );

        //刷新最低价，和最高价
        List<QuotationProduct> quotationProductList = quotationProductService.list(q -> q.eq(QuotationProduct::getSaleQuotationId, businessId));
        for (QuotationProduct quotationProduct : quotationProductList) {
            BigDecimal minPrice = quotationProduct.getMinPrice();
            BigDecimal maxPrice = quotationProduct.getMaxPrice();
            BigDecimal discussPrice = quotationProduct.getDiscussPrice();

            //修改最低价
            if (discussPrice.compareTo(minPrice) < 0) {
                quotationProduct.setMinPrice(discussPrice);
            }

            //修改最高价
            if (discussPrice.compareTo(maxPrice) > 0) {
                quotationProduct.setMaxPrice(discussPrice);
            }
        }
        quotationProductService.updateBatchById(quotationProductList);
    }

}
