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.ErpBaseMaterial;
import vip.xiaonuo.sale.entity.SaleProduce;
import vip.xiaonuo.sale.mapper.ErpBaseMaterialMapper;
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.DecimalFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.ArrayList;
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;
    @Autowired
    private ErpBaseMaterialMapper materialMapper;

    @Override
    public JSONObject getProduceOne(SaleProduceParam produceParam) {
        Integer parentId = produceParam.getParentId();
        String time = produceParam.getDate();
        String year = time.substring(0, 4);
        QueryWrapper<ErpBaseMaterial> materialQueryWrapper = new QueryWrapper<>();
        materialQueryWrapper.eq("parent_id", parentId);
        List<ErpBaseMaterial> materials = materialMapper.selectList(materialQueryWrapper);
        Double monthSaleMoney = 0.00;
        Double yearSaleMoney = 0.00;
        Double monthPrice = 0.00;
        Double yearPrice = 0.00;
        if (materials.size() != 0) {
            for (ErpBaseMaterial material : materials) {
                QueryWrapper<SaleProduce> queryWrapper = new QueryWrapper<>();
                queryWrapper.like("produce_name", material.getMaterialName());
                queryWrapper.between("create_time", time + "-01 00:00:00", time + "-31 23:59:59");
                queryWrapper.select(" IFNULL(sum(sale_money),'0') saleMoney,convert(IFNULL(avg(price),'0'),decimal(10,2) ) price");
                SaleProduce monthProduce = produceMapper.selectOne(queryWrapper);
                QueryWrapper<SaleProduce> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.like("produce_name", material.getMaterialName());
                queryWrapper1.between("create_time", year + "-01-01 00:00:00", year + "-12-31 23:59:59");
                queryWrapper1.select(" IFNULL(sum(sale_money),'0') saleMoney,convert(IFNULL(avg(price),'0'),decimal(10,2) ) price");
                SaleProduce yearProduce = produceMapper.selectOne(queryWrapper1);

                monthSaleMoney = monthSaleMoney + Double.valueOf(monthProduce.getSaleMoney());
                yearSaleMoney = yearSaleMoney + Double.valueOf(yearProduce.getSaleMoney());
                monthPrice = monthPrice + Double.valueOf(monthProduce.getPrice());
                yearPrice = yearPrice + Double.valueOf(yearProduce.getPrice());
            }
        }
        DecimalFormat def = new DecimalFormat("0.00");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("monthPrice", def.format(monthSaleMoney));
        jsonObject.put("yearPrice",def.format(yearSaleMoney));
        if (materials.size() != 0) {
            double v = monthPrice / materials.size();
            double v1 = yearPrice / materials.size();
            jsonObject.put("monthSaleMoney", def.format(v));
            jsonObject.put("yearSaleMoney", def.format(v1));
        } else {
            jsonObject.put("monthSaleMoney", "0");
            jsonObject.put("yearSaleMoney", "0");
        }
        return jsonObject;
    }

    @Override
    public JSONArray getPriceList(SaleProduceParam produceParam) {
        Integer parentId = produceParam.getParentId();
        String time = produceParam.getDate();
        String year = time.substring(0, 4);
        int last = Integer.parseInt(year) - 1;
        QueryWrapper<ErpBaseMaterial> materialQueryWrapper = new QueryWrapper<>();
        materialQueryWrapper.eq("parent_id", parentId);
        List<ErpBaseMaterial> materials = materialMapper.selectList(materialQueryWrapper);
     /*   List<SaleProduce> yearList = new ArrayList<>();
        List<SaleProduce> lastYearList = new ArrayList<>();
        if (materials.size() != 0) {
            for (ErpBaseMaterial material : materials) {
                QueryWrapper<SaleProduce> queryWrapper = new QueryWrapper<>();
                queryWrapper.like("produce_name", material.getMaterialName());
                queryWrapper.between("create_time", year + "-01-01 00:00:00", year + "-12-31 23:59:59");
                queryWrapper.select(" sum(sale_money) saleMoney,create_time createTime");
                queryWrapper.groupBy("DATE_FORMAT(create_time,'%Y-%m')");
                List<SaleProduce> lastProduce = produceMapper.selectList(queryWrapper);


                QueryWrapper<SaleProduce> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.like("produce_name", material.getMaterialName());
                queryWrapper1.between("create_time", last + "-01-01 00:00:00", last + "-12-31 23:59:59");
                queryWrapper1.select(" sum(sale_money) saleMoney,create_time createTime");
                queryWrapper1.groupBy("DATE_FORMAT(create_time,'%Y-%m')");
                List<SaleProduce> yearProduce = 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");

            Double yearMoney = 0.00;
            Double lastYearMoney = 0.00;
            for (ErpBaseMaterial material : materials) {
                QueryWrapper<SaleProduce> queryWrapper = new QueryWrapper<>();
                queryWrapper.like("produce_name", material.getMaterialName());
                queryWrapper.between("create_time", year + "-"+i+"-01 00:00:00", year +"-"+i+"-31 23:59:59");
                queryWrapper.select(" IFNULL(sum(sale_money),'0') saleMoney,create_time createTime");
                SaleProduce saleProduce = produceMapper.selectOne(queryWrapper);
                yearMoney = yearMoney + Double.valueOf(saleProduce.getSaleMoney());

                QueryWrapper<SaleProduce> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.like("produce_name", material.getMaterialName());
                queryWrapper1.between("create_time", last + "-"+i+"-01 00:00:00", last +"-"+i+"-31 23:59:59");
                queryWrapper1.select(" IFNULL(sum(sale_money),'0') saleMoney,create_time createTime");
                SaleProduce yearProduce = produceMapper.selectOne(queryWrapper1);
                lastYearMoney = lastYearMoney + Double.valueOf(yearProduce.getSaleMoney());
            }
            DecimalFormat def = new DecimalFormat("0.00");
            jsonObject.put("year", def.format(yearMoney));
            jsonObject.put("lastYear", def.format(lastYearMoney));
            /*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;
    }
}
