package com.yh.saas.plugin.yiliangyiyun.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.yh.saas.plugin.yiliangyiyun.entity.PurchaseOrder;
import com.yh.saas.plugin.yiliangyiyun.entity.SaleOrder;
import com.yh.saas.plugin.yiliangyiyun.entity.TransactionRecord;
import com.yh.saas.plugin.yiliangyiyun.entity.view.ListView;
import com.yh.saas.plugin.yiliangyiyun.mapper.TransactionRecordMapper;
import com.yh.saas.plugin.yiliangyiyun.service.IPurchaseOrderService;
import com.yh.saas.plugin.yiliangyiyun.service.ISaleOrderService;
import com.yh.saas.plugin.yiliangyiyun.service.ITransactionRecordService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 成交记录 服务实现类
 * </p>
 *
 * @author Gongdc
 * @since 2021-07-22
 */
@Service
public class TransactionRecordServiceImpl extends ServiceImpl<TransactionRecordMapper, TransactionRecord> implements ITransactionRecordService {
    @Autowired
    private IPurchaseOrderService purchaseOrderService;
    @Autowired
    private ISaleOrderService saleOrderService;
    /**
     * 获取成交价列表
     * @param id
     * @return
     */
    @Override
    public List<ListView> getPriceList(String id) {
        List<ListView> priceList = new ArrayList<>();
        PurchaseOrder purchaseOrder = purchaseOrderService.selectById(id);
        //查询成交记录
        List<TransactionRecord> transactionRecordList = this.selectList(new EntityWrapper<TransactionRecord>()
                .eq("order_id", purchaseOrder.getId())
                .eq("record_flag", "0")
                .eq("delete_flag", "0")
                .groupBy("transaction_price"));
        if (!CollectionUtils.isEmpty(transactionRecordList)) {
            for (TransactionRecord transactionRecord : transactionRecordList) {
                List<TransactionRecord> transactionRecordList1 = this.selectList(new EntityWrapper<TransactionRecord>()
                        .eq("order_id", purchaseOrder.getId())
                        .eq("transaction_price", transactionRecord.getTransactionPrice())
                        .eq("record_flag", "0")
                        .eq("delete_flag", "0"));
                ListView listView =  new ListView();
                Float turnover = 0f;
                Float turnoverClose = 0f;
                if (!CollectionUtils.isEmpty(transactionRecordList1)) {
                    for (TransactionRecord transactionRecord1 : transactionRecordList1) {
                        turnover = turnover + transactionRecord1.getTurnover();

                    }
                }
                List<TransactionRecord> transactionRecordList2 = this.selectList(new EntityWrapper<TransactionRecord>()
                        .eq("order_id", purchaseOrder.getId())
                        .eq("transaction_price", transactionRecord.getTransactionPrice())
                        .eq("record_flag", "1")
                        .eq("delete_flag", "0"));
                if (!CollectionUtils.isEmpty(transactionRecordList1)) {
                    for (TransactionRecord transactionRecord1 : transactionRecordList2) {
                        turnoverClose = turnoverClose + transactionRecord1.getTurnover();
                    }
                    Float price = turnover - turnoverClose;
                    listView.setKey(transactionRecord.getTransactionPrice());
                    listView.setValue(price);
                    priceList.add(listView);
                }
            }
        }
        return priceList;
    }

    /**
     * 获取成交价列表销售
     * @param id
     * @return
     */
    @Override
    public List<ListView> getSalePriceList(String id) {
        List<ListView> priceList = new ArrayList<>();
        SaleOrder saleOrder = saleOrderService.selectById(id);
        //查询成交记录
        List<TransactionRecord> transactionRecordList = this.selectList(new EntityWrapper<TransactionRecord>()
                .eq("order_id", saleOrder.getId())
                .eq("record_flag", "0")
                .eq("delete_flag", "0")
                .groupBy("transaction_price"));
        if (!CollectionUtils.isEmpty(transactionRecordList)) {
            for (TransactionRecord transactionRecord : transactionRecordList) {
                List<TransactionRecord> transactionRecordList1 = this.selectList(new EntityWrapper<TransactionRecord>()
                        .eq("order_id", saleOrder.getId())
                        .eq("transaction_price", transactionRecord.getTransactionPrice())
                        .eq("record_flag", "0")
                        .eq("delete_flag", "0"));
                ListView listView =  new ListView();
                Float turnover = 0f;
                Float turnoverClose = 0f;
                if (!CollectionUtils.isEmpty(transactionRecordList1)) {
                    for (TransactionRecord transactionRecord1 : transactionRecordList1) {
                        turnover = turnover + transactionRecord1.getTurnover();

                    }
                }
                List<TransactionRecord> transactionRecordList2 = this.selectList(new EntityWrapper<TransactionRecord>()
                        .eq("order_id", saleOrder.getId())
                        .eq("transaction_price", transactionRecord.getTransactionPrice())
                        .eq("record_flag", "1")
                        .eq("delete_flag", "0"));
                if (!CollectionUtils.isEmpty(transactionRecordList1)) {
                    for (TransactionRecord transactionRecord1 : transactionRecordList2) {
                        turnoverClose = turnoverClose + transactionRecord1.getTurnover();
                    }
                    Float price = turnover - turnoverClose;
                    listView.setKey(transactionRecord.getTransactionPrice());
                    listView.setValue(price);
                    priceList.add(listView);
                }
            }
        }
        return priceList;
    }
}
