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.SaleProduce;
import vip.xiaonuo.sale.mapper.SaleProduceMapper;
import vip.xiaonuo.sale.param.SaleProduceParam;
import vip.xiaonuo.sale.service.ISaleProduceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 销售二级指标 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-12-13
 */
@Service
public class SaleProduceServiceImpl extends ServiceImpl<SaleProduceMapper, SaleProduce> implements ISaleProduceService {

    @Autowired
    private SaleProduceMapper produceMapper;

    @Override
    public JSONObject getProduceOne(SaleProduceParam produceParam) {
        String orgId = produceParam.getOrgId();
        String produceName = produceParam.getProduceName();
        QueryWrapper<SaleProduce> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(orgId), "org_id", orgId);
        queryWrapper.eq(StringUtils.isNotBlank(produceName), "produce_name", produceName);
        queryWrapper.ge("create_time", DateUtil.beginOfMonth(new Date()));
        queryWrapper.select(" IFNULL(sum(sale_money),'0') saleMoney,convert(IFNULL(avg(price),'0'),decimal(10,2))");
        SaleProduce monthProduce = produceMapper.selectOne(queryWrapper);
        QueryWrapper<SaleProduce> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq(StringUtils.isNotBlank(orgId), "org_id", orgId);
        queryWrapper1.eq(StringUtils.isNotBlank(produceName), "produce_name", produceName);
        queryWrapper1.ge("create_time", DateUtil.beginOfYear(new Date()));
        queryWrapper1.select(" IFNULL(sum(sale_money),'0') saleMoney,convert(IFNULL(avg(price),'0'),decimal(10,2))");
        SaleProduce yearProduce = produceMapper.selectOne(queryWrapper1);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("monthSaleMoney", monthProduce.getSaleMoney());
        jsonObject.put("monthPrice", monthProduce.getPrice());
        jsonObject.put("yearSaleMoney", yearProduce.getSaleMoney());
        jsonObject.put("yearPrice", yearProduce.getPrice());
        return jsonObject;
    }

    @Override
    public JSONArray getPriceList(SaleProduceParam produceParam) {
        String orgId = produceParam.getOrgId();
        String produceName = produceParam.getProduceName();
        QueryWrapper<SaleProduce> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(orgId), "org_id", orgId);
        queryWrapper.eq(StringUtils.isNotBlank(produceName), "produce_name", produceName);
        queryWrapper.ge("create_time", DateUtil.beginOfYear(new Date()));
        queryWrapper.select(" sum(sale_money) saleMoney,create_time createTime");
        queryWrapper.groupBy("DATE_FORMAT(create_time,'%Y-%m')");
        List<SaleProduce> yearList = produceMapper.selectList(queryWrapper);

        LocalDate currentDate = LocalDate.now();
        int lastYear = currentDate.getYear() - 1;
        QueryWrapper<SaleProduce> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq(StringUtils.isNotBlank(orgId), "org_id", orgId);
        queryWrapper1.eq(StringUtils.isNotBlank(produceName), "produce_name", produceName);
        queryWrapper1.between("create_time", lastYear + "-01-01 00:00:00", lastYear + "-12-31 23:59:59");
        queryWrapper1.select(" sum(sale_money) saleMoney,create_time createTime");
        queryWrapper.groupBy("DATE_FORMAT(create_time,'%Y-%m')");
        List<SaleProduce> lastYearList = produceMapper.selectList(queryWrapper1);
        SimpleDateFormat sdf = new SimpleDateFormat("MM");
        JSONArray jsonArray = new JSONArray();
        for (int i = 1; i <13 ; i++) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("time", i+"月");
            jsonObject.put("year", "0");
            jsonObject.put("lastYear", "0");
            if (yearList.size() != 0) {
                for (SaleProduce produce : yearList) {
                    String format = sdf.format(produce.getCreateTime());
                    if (i == Integer.parseInt(format)) {
                        jsonObject.put("year", produce.getSaleMoney());
                    }
                }
            } else {
                jsonObject.put("year", "0");
            }
            if (lastYearList.size() != 0) {
                for (SaleProduce produce : lastYearList) {
                    String format = sdf.format(produce.getCreateTime());
                    if (i == Integer.parseInt(format)) {
                        jsonObject.put("lastYear", produce.getSaleMoney());
                    }
                }
            } else {
                jsonObject.put("lastYear", "0");
            }
            jsonArray.add(jsonObject);
        }
        return jsonArray;
    }
}
