package vip.xiaonuo.risk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import vip.xiaonuo.purchase.entity.PurchaseOrder;
import vip.xiaonuo.purchase.mapper.PurchaseOrderMapper;
import vip.xiaonuo.risk.entity.RiskTarget;
import vip.xiaonuo.risk.mapper.RiskTargetMapper;
import vip.xiaonuo.risk.param.RiskParam;
import vip.xiaonuo.risk.service.IRiskTargetService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import vip.xiaonuo.sale.entity.SaleDeliver;
import vip.xiaonuo.sale.entity.SaleDetail;
import vip.xiaonuo.sale.mapper.SaleDeliverMapper;
import vip.xiaonuo.sale.mapper.SaleDetailMapper;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * <p>
 * 风控指标 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-12-09
 */
@Service
public class RiskTargetServiceImpl extends ServiceImpl<RiskTargetMapper, RiskTarget> implements IRiskTargetService {

    @Autowired
    private RiskTargetMapper riskTargetMapper;
    @Autowired
    private PurchaseOrderMapper orderMapper;
    @Autowired
    private SaleDetailMapper detailMapper;
    @Autowired
    private SaleDeliverMapper deliverMapper;

    @Override
    public RiskTarget getOneRisk(RiskParam riskParam) {
        String orgId = riskParam.getOrgId();
        String time = riskParam.getTime();//2025-01
        String year = time.substring(0, 4);
        RiskTarget target = new RiskTarget();
        QueryWrapper<PurchaseOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(orgId), "org_id", orgId);
        queryWrapper.between("create_time", time + "-01 00:00:00", time + "-31 23:59:59");
        queryWrapper.select(" IFNULL(sum(purchase_money),'0') purchaseMoney,IFNULL(sum(material_accept_price),'0') materialAcceptPrice" +
                ",IFNULL(sum(enter_price),'0') enterPrice");
        PurchaseOrder purchaseOrder = orderMapper.selectOne(queryWrapper);//yue
        queryWrapper.clear();
        queryWrapper.eq(StringUtils.isNotBlank(orgId), "org_id", orgId);
        queryWrapper.between("create_time", Integer.parseInt(year) + "-01-01 00:00:00", Integer.parseInt(year) + "-12-31 23:59:59");
        queryWrapper.select(" IFNULL(sum(purchase_money),'0') purchaseMoney,IFNULL(sum(material_accept_price),'0') materialAcceptPrice" +
                ",IFNULL(sum(enter_price),'0') enterPrice");
        PurchaseOrder purchaseOrder1 = orderMapper.selectOne(queryWrapper);//nian



        QueryWrapper<SaleDetail> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq(StringUtils.isNotBlank(orgId), "org_id", orgId);
        queryWrapper1.between("create_time", time + "-01 00:00:00", time + "-31 23:59:59");
        queryWrapper1.select(" IFNULL(sum(sale_money),'0') saleMoney,IFNULL(count(*),'0') saleNumber");
        SaleDetail saleDetail = detailMapper.selectOne(queryWrapper1);//yue
        queryWrapper1.clear();
        queryWrapper1.eq(StringUtils.isNotBlank(orgId), "org_id", orgId);
        queryWrapper1.between("create_time", Integer.parseInt(year) + "-01-01 00:00:00", Integer.parseInt(year) + "-12-31 23:59:59");
        queryWrapper1.select(" IFNULL(sum(sale_money),'0') saleMoney,IFNULL(count(*),'0') saleNumber");
        SaleDetail saleDetail1 = detailMapper.selectOne(queryWrapper1);

        QueryWrapper<SaleDeliver> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq(StringUtils.isNotBlank(orgId), "org_id", orgId);
        queryWrapper2.between("create_time", time + "-01 00:00:00", time + "-31 23:59:59");
        queryWrapper2.select(" IFNULL(sum(sale_deliver_money),'0') saleDeliverMoney");
        SaleDeliver saleDeliver = deliverMapper.selectOne(queryWrapper2);
        queryWrapper2.clear();
        queryWrapper2.eq(StringUtils.isNotBlank(orgId), "org_id", orgId);
        queryWrapper2.between("create_time", Integer.parseInt(year) + "-01-01 00:00:00", Integer.parseInt(year) + "-12-31 23:59:59");
        queryWrapper2.select(" IFNULL(sum(sale_deliver_money),'0') saleDeliverMoney");
        SaleDeliver saleDeliver1 = deliverMapper.selectOne(queryWrapper2);

        BigDecimal math = new BigDecimal("10000");
        BigDecimal bg = new BigDecimal(purchaseOrder.getPurchaseMoney());
        target.setMonthOrderMoney(bg.divide(math, 2, RoundingMode.HALF_UP).toString());
        BigDecimal bg1 = new BigDecimal(purchaseOrder1.getPurchaseMoney());
        target.setYearOrderMoney(bg1.divide(math, 2, RoundingMode.HALF_UP).toString());
        BigDecimal bg2 = new BigDecimal(purchaseOrder.getMaterialAcceptPrice());
        target.setMonthMaterialMoney(bg2.divide(math, 2, RoundingMode.HALF_UP).toString());
        BigDecimal bg3 = new BigDecimal(purchaseOrder1.getMaterialAcceptPrice());
        target.setYearMaterialMoney(bg3.divide(math, 2, RoundingMode.HALF_UP).toString());
        BigDecimal bg4 = new BigDecimal(purchaseOrder.getEnterPrice());
        target.setMonthIncomeMoney(bg4.divide(math, 2, RoundingMode.HALF_UP).toString());
        BigDecimal bg5 = new BigDecimal(purchaseOrder1.getEnterPrice());
        target.setYearIncomeMoney(bg5.divide(math, 2, RoundingMode.HALF_UP).toString());
        target.setMonthSaleNumber(saleDetail.getSaleNumber());
        target.setYearSaleNumber(saleDetail1.getSaleNumber());
        BigDecimal bg6 = new BigDecimal(saleDetail.getSaleMoney());
        target.setMonthSaleMoney(bg6.divide(math, 2, RoundingMode.HALF_UP).toString());
        BigDecimal bg7 = new BigDecimal(saleDetail1.getSaleMoney());
        target.setYearSaleMoney(bg7.divide(math, 2, RoundingMode.HALF_UP).toString());
        BigDecimal bg8 = new BigDecimal(saleDeliver.getSaleDeliverMoney());
        target.setMonthDeliverMoney(bg8.divide(math, 2, RoundingMode.HALF_UP).toString());
        BigDecimal bg9 = new BigDecimal(saleDeliver1.getSaleDeliverMoney());
        target.setYearDeliverMoney(bg9.divide(math, 2, RoundingMode.HALF_UP).toString());
        return target;
    }

}
