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.SaleTarget;
import vip.xiaonuo.sale.mapper.SaleTargetMapper;
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.util.Date;

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

    @Autowired
    private SaleTargetMapper targetMapper;

    @Override
    public JSONArray getTarget(SaleParam saleParam) {
        String orgId = saleParam.getOrgId();
        String time = saleParam.getTime();
        QueryWrapper<SaleTarget> queryWrapper = new QueryWrapper<>();
        QueryWrapper<SaleTarget> queryWrapper1 = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(orgId), "org_id", orgId);
        queryWrapper1.eq(StringUtils.isNotBlank(orgId), "org_id", orgId);
        queryWrapper.select(" convert(IFNULL(avg(complete_rate),'0'),decimal(10,2)) completeRate,IFNULL(sum(visit_number),'0') visitNumber," +
                "IFNULL(sum(contract_money),'0') contractMoney,IFNULL(sum(sale_order_money),'0') saleOrderMoney,IFNULL(sum(sale_deliver_money),'0') saleDeliverMoney," +
                "IFNULL(sum(sale_invoice_money),'0') saleInvoiceMoney");
        queryWrapper1.select(" convert(IFNULL(avg(complete_rate),'0'),decimal(10,2)) completeRate,IFNULL(sum(visit_number),'0') visitNumber," +
                "IFNULL(sum(contract_money),'0') contractMoney,IFNULL(sum(sale_order_money),'0') saleOrderMoney,IFNULL(sum(sale_deliver_money),'0') saleDeliverMoney," +
                "IFNULL(sum(sale_invoice_money),'0') saleInvoiceMoney");
        if (StringUtils.isNotBlank(time)) {
            queryWrapper.between("create_time", time + "-01 00:00:00", time + "-31 23:59:59");
            queryWrapper1.between("create_time", time+"-01 00:00:00", time + "-31 23:59:59");
        } else {
            queryWrapper.ge("create_time", DateUtil.beginOfMonth(new Date()));
            queryWrapper1.ge("create_time", DateUtil.beginOfYear(new Date()));
        }
        SaleTarget monthTarget = targetMapper.selectOne(queryWrapper);
        SaleTarget yearTarget = targetMapper.selectOne(queryWrapper1);
        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;
    }
}
