package vip.xiaonuo.sale.service.impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
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.sale.entity.SaleDeliver;
import vip.xiaonuo.sale.entity.SaleInvoice;
import vip.xiaonuo.sale.entity.SaleOrder;
import vip.xiaonuo.sale.entity.SaleTarget;
import vip.xiaonuo.sale.mapper.*;
import vip.xiaonuo.sale.param.SaleParam;
import vip.xiaonuo.sale.service.ISaleTargetService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 销售指标 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-12-13
 */
@Service
public class SaleTargetServiceImpl extends ServiceImpl<SaleTargetMapper, SaleTarget> implements ISaleTargetService {

    @Autowired
    private SaleTargetMapper targetMapper;
    @Autowired
    private SaleInvoiceMapper invoiceMapper;
    @Autowired
    private SaleDeliverMapper deliverMapper;
    @Autowired
    private SaleOrderMapper orderMapper;

    @Override
    public JSONArray getTarget(SaleParam saleParam) {
        String orgId = saleParam.getOrgId();
        String time = saleParam.getTime();
        String year = time.substring(0, 4);

        //销售发货金额
        QueryWrapper<SaleDeliver> deliverQueryWrapper = new QueryWrapper<>();
        deliverQueryWrapper.eq(StringUtils.isNotBlank(orgId), "org_id", orgId);
        deliverQueryWrapper.between("create_time", time + "-01 00:00:00", time + "-31 23:59:59");
        deliverQueryWrapper.select(" IFNULL(sum(sale_deliver_money),'0') saleDeliverMoney");
        SaleDeliver monthDeliver = deliverMapper.selectOne(deliverQueryWrapper);
        deliverQueryWrapper.clear();
        deliverQueryWrapper.eq(StringUtils.isNotBlank(orgId), "org_id", orgId);
        deliverQueryWrapper.between("create_time", year + "-01-01 00:00:00", year + "-12-31 23:59:59");
        deliverQueryWrapper.select(" IFNULL(sum(sale_deliver_money),'0') saleDeliverMoney");
        SaleDeliver yearDeliver = deliverMapper.selectOne(deliverQueryWrapper);
        //销售发票金额
        QueryWrapper<SaleInvoice> invoiceQueryWrapper = new QueryWrapper<>();
        invoiceQueryWrapper.eq(StringUtils.isNotBlank(orgId), "org_id", orgId);
        invoiceQueryWrapper.between("create_time", time + "-01 00:00:00", time + "-31 23:59:59");
        invoiceQueryWrapper.select(" IFNULL(sum(sale_invoice_money),'0') saleInvoiceMoney");
        SaleInvoice monthInvoice = invoiceMapper.selectOne(invoiceQueryWrapper);
        invoiceQueryWrapper.clear();
        invoiceQueryWrapper.eq(StringUtils.isNotBlank(orgId), "org_id", orgId);
        invoiceQueryWrapper.between("create_time", year + "-01-01 00:00:00", year + "-12-31 23:59:59");
        invoiceQueryWrapper.select(" IFNULL(sum(sale_invoice_money),'0') saleInvoiceMoney");
        SaleInvoice yearInvoice = invoiceMapper.selectOne(invoiceQueryWrapper);
        //销售订单金额和合同金额
        QueryWrapper<SaleOrder> orderQueryWrapper = new QueryWrapper<>();
        orderQueryWrapper.eq(StringUtils.isNotBlank(orgId), "org_id", orgId);
        orderQueryWrapper.between("create_time", time + "-01 00:00:00", time + "-31 23:59:59");
        orderQueryWrapper.select(" IFNULL(sum(sale_money),'0') saleMoney");
        SaleOrder monthOrder = orderMapper.selectOne(orderQueryWrapper);
        orderQueryWrapper.clear();
        orderQueryWrapper.eq(StringUtils.isNotBlank(orgId), "org_id", orgId);
        orderQueryWrapper.between("create_time", year + "-01-01 00:00:00", year + "-12-31 23:59:59");
        orderQueryWrapper.select(" IFNULL(sum(sale_money),'0') saleMoney");
        SaleOrder yearOrder = orderMapper.selectOne(orderQueryWrapper);


        QueryWrapper<SaleTarget> 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(visit_number),'0') visitNumber");
        SaleTarget monthTarget = targetMapper.selectOne(queryWrapper);
        queryWrapper.clear();
        queryWrapper.eq(StringUtils.isNotBlank(orgId), "org_id", orgId);
        queryWrapper.between("create_time", year + "-01-01 00:00:00", year + "-12-31 23:59:59");
        queryWrapper.select(" IFNULL(sum(visit_number),'0') visitNumber");
        SaleTarget yearTarget = targetMapper.selectOne(queryWrapper);


        BigDecimal math = new BigDecimal("10000");
        BigDecimal bg = new BigDecimal(monthOrder.getSaleMoney());
        BigDecimal bg1 = new BigDecimal(yearOrder.getSaleMoney());
        BigDecimal bg2 = new BigDecimal(monthDeliver.getSaleDeliverMoney());
        BigDecimal bg3 = new BigDecimal(yearDeliver.getSaleDeliverMoney());
        BigDecimal bg4 = new BigDecimal(monthInvoice.getSaleInvoiceMoney());
        BigDecimal bg5 = new BigDecimal(yearInvoice.getSaleInvoiceMoney());

        monthTarget.setCompleteRate("0");
        yearTarget.setCompleteRate("0");
        monthTarget.setContractMoney(bg.divide(math, 2, RoundingMode.HALF_UP).toString());
        yearTarget.setContractMoney(bg1.divide(math, 2, RoundingMode.HALF_UP).toString());
        monthTarget.setSaleOrderMoney(bg.divide(math, 2, RoundingMode.HALF_UP).toString());
        yearTarget.setSaleOrderMoney(bg1.divide(math, 2, RoundingMode.HALF_UP).toString());
        monthTarget.setSaleDeliverMoney(bg2.divide(math, 2, RoundingMode.HALF_UP).toString());
        yearTarget.setSaleDeliverMoney(bg3.divide(math, 2, RoundingMode.HALF_UP).toString());
        monthTarget.setSaleInvoiceMoney(bg4.divide(math, 2, RoundingMode.HALF_UP).toString());
        yearTarget.setSaleInvoiceMoney(bg5.divide(math, 2, RoundingMode.HALF_UP).toString());
        JSONArray jsonArray = new JSONArray();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("type", "month");
        jsonObject.put("object", monthTarget);
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("type", "year");
        jsonObject1.put("object", yearTarget);
        jsonArray.add(jsonObject);
        jsonArray.add(jsonObject1);
        return jsonArray;
    }
}
