package com.huimv.produce.produce.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huimv.common.utils.*;
import com.huimv.produce.produce.entity.ProdSales;
import com.huimv.produce.produce.entity.dto.ClientHomeDto;
import com.huimv.produce.produce.entity.dto.ProdSalesDto;
import com.huimv.produce.produce.mapper.ProdSalesMapper;
import com.huimv.produce.produce.service.IProdSalesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author astupidcoder
 * @since 2021-11-25
 */
@Service
public class ProdSalesServiceImpl extends ServiceImpl<ProdSalesMapper, ProdSales> implements IProdSalesService {

    @Autowired
    private ProdSalesMapper salesMapper;

//    @Override
    public Result listSales_yuxuexuan(Map map) {
        Integer farmId = (Integer)map.get("farmId");
        Integer type = (Integer)map.get("type");
        String startDate = (String)map.get("startDate");
        String endDate = (String)map.get("endDate");
        QueryWrapper<ProdSales> wrapper = new QueryWrapper<>();
        wrapper.eq("farm_id",farmId);
        Map endMap = new HashMap();
        List<ProdSales> list = new ArrayList<>();
        //本周
        if (1==type){
            wrapper.ge("add_date", DateUtil.beginOfWeek(new Date()));
            wrapper.isNotNull("today_quantity");
            list = this.list(wrapper);
        }//自定义
        else if (3==type) {
            endDate = endDate+" 23:59:59";
            startDate = startDate+" 00:00:00";
            wrapper.between("add_date",startDate,endDate);
            wrapper.isNotNull("today_quantity");
            list = this.list(wrapper);
        }
        //本月
        else {
            wrapper.ge("add_date", DataUill.getTimesMonthmorning());
            wrapper.isNotNull("today_quantity");
            list = this.list(wrapper);
        }
        wrapper.clear();
        wrapper.eq("farm_id",farmId);
        wrapper.last("AND PERIOD_DIFF( DATE_FORMAT( NOW( ) , '%Y%m' ) , DATE_FORMAT( add_date, '%Y%m' ) ) =1  ");
        ProdSalesDto lastProdSales = salesMapper.countMonth(wrapper);
        wrapper.clear();
        wrapper.eq("farm_id",farmId);
        wrapper.last("AND PERIOD_DIFF( DATE_FORMAT( NOW( ) , '%Y%m' ) , DATE_FORMAT( add_date, '%Y%m' ) ) =2  ");
        ProdSalesDto lastTwoProdSales = salesMapper.countMonth(wrapper);
        List timeList = new ArrayList();
        List moneyList = new ArrayList();
        List quantityList = new ArrayList();
        List moneyList1 = new ArrayList();
        for (ProdSales prodSales : list) {
            timeList.add(DateUtil.format(prodSales.getAddDate(),"MM-dd"));
            moneyList.add(  prodSales.getTodayMoney());
            quantityList.add(  prodSales.getTodayQuantity());
            moneyList1.add( NumberUtils.format( prodSales.getTodayMoney()));
        }
        wrapper.clear();
        wrapper.eq("farm_id",farmId);
        wrapper.ge("add_date",DateUtil.beginOfMonth(new Date()));
        ProdSalesDto prodSalesDto = salesMapper.countMonth(wrapper);
        wrapper.clear();
        wrapper.ge("add_date",DateUtil.beginOfMonth(new Date()));
        wrapper.orderByDesc("add_date");
        wrapper.eq("farm_id",farmId);
        wrapper.last("limit 1");
        ProdSales one = this.getOne(wrapper);
        if (ObjectUtil.isNotEmpty(one)){
            //本月
            endMap.put("nowMoney", 0);
            endMap.put("nowQuantity", 0);
            if (one.getMonthMoney()!=null){
                endMap.put("nowMoney", one.getMonthMoney());
            }
            if (one.getMonthQuantity()!=null){
                endMap.put("nowQuantity", one.getMonthQuantity());
            }


            endMap.put("timeList",timeList);
            endMap.put("moneyList",moneyList);
            endMap.put("quantityList",quantityList);
            endMap.put("moneyList1",moneyList1);
        }else {
            endMap.put("nowMoney", 0);
            endMap.put("nowQuantity", 0);

            endMap.put("timeList",null);
            endMap.put("moneyList",null);
            endMap.put("quantityList",null);
            endMap.put("moneyList1",null);
        }
        //上月
        wrapper.clear();
        wrapper.lt("add_date",DateUtil.beginOfMonth(new Date()));
        wrapper.orderByDesc("add_date");
        wrapper.eq("farm_id",farmId);
        wrapper.last("limit 1");
        ProdSales lastone = this.getOne(wrapper);
        if (ObjectUtil.isNotEmpty(lastone)){
            endMap.put("quantity",lastone.getMonthQuantity());
            endMap.put("quantityPercent", PercentUtil.getGrowth(lastProdSales.getQuantity(),lastTwoProdSales.getQuantity()));
            endMap.put("money", NumberUtils.format(lastone.getMonthMoney()+""));
            endMap.put("moneyPercent", PercentUtil.getGrowth(lastProdSales.getMoney(),lastTwoProdSales.getMoney()));
        }else {
            endMap.put("quantity",0);
            endMap.put("quantityPercent", 0);
            endMap.put("money", 0);
            endMap.put("moneyPercent", 0);
        }
        return new Result(ResultCode.SUCCESS,endMap);
    }

    @Override
    public Result listSales(Map map) {
        // 查询上月最后一天数据
        Map lastOneMonthSalesMap = getLastXMonth(map,1);
        System.out.println("1="+lastOneMonthSalesMap.toString());
        String lastOneMonthSalesMoney = "0";
        String lastOneMonthSum = "0";
        if(lastOneMonthSalesMap.size() >0){
            lastOneMonthSalesMoney = lastOneMonthSalesMap.get("monthMoney")+"";
            lastOneMonthSum = lastOneMonthSalesMap.get("monthQuantity")+"";
        }
        System.out.println("lastOneMonthSalesMoney="+lastOneMonthSalesMoney);
        System.out.println("lastOneMonthSum="+lastOneMonthSum);

//        BigDecimal lastOneMonthMoney = new BigDecimal(lastOneMonthSalesMap.get("")+"");
        // 查询上上月最后一天数据
        Map lastTwoMonthSalesMap = getLastXMonth(map,2);
        String lastTwoMonthSalesMoney = "0";
        String lastTwoMonthSum = "0";
        System.out.println("2="+lastTwoMonthSalesMap.toString());
        if(lastTwoMonthSalesMap.size()>0){
            lastTwoMonthSalesMoney = lastTwoMonthSalesMap.get("monthMoney")+"";
            lastTwoMonthSum = lastTwoMonthSalesMap.get("monthQuantity")+"";
        }
        System.out.println("lastTwoMonthSalesMoney="+lastTwoMonthSalesMoney);
        System.out.println("lastTwoMonthSum="+lastTwoMonthSum);

        PercentUtil.getGrowth(Double.parseDouble(lastOneMonthSalesMoney),Double.parseDouble(lastTwoMonthSalesMoney));

        Integer farmId = (Integer)map.get("farmId");
        Integer type = (Integer)map.get("type");
        String startDate = (String)map.get("startDate");
        String endDate = (String)map.get("endDate");
        QueryWrapper<ProdSales> wrapper = new QueryWrapper<>();
        wrapper.eq("farm_id",farmId);
            Map endMap = new HashMap();
            endMap.put("quantity",lastOneMonthSum);
            endMap.put("quantityPercent", PercentUtil.getGrowth(Double.parseDouble(lastOneMonthSum),Double.parseDouble(lastTwoMonthSum)));
            endMap.put("money", NumberUtils.format(lastOneMonthSalesMoney+""));
            endMap.put("moneyPercent", PercentUtil.getGrowth(Double.parseDouble(lastOneMonthSalesMoney),Double.parseDouble(lastTwoMonthSalesMoney)));
        List<ProdSales> list = new ArrayList<>();
        //本周
        if (1==type){
            wrapper.ge("add_date", DateUtil.beginOfWeek(new Date()));
            wrapper.isNotNull("today_quantity");
            list = this.list(wrapper);
        }//自定义
        else if (3==type) {
            endDate = endDate+" 23:59:59";
            startDate = startDate+" 00:00:00";
            wrapper.between("add_date",startDate,endDate);
            wrapper.isNotNull("today_quantity");
            list = this.list(wrapper);
        }
        //本月
        else {
            wrapper.ge("add_date", DateUtil.beginOfMonth(new Date()));
            wrapper.isNotNull("today_quantity");
            list = this.list(wrapper);
        }
        wrapper.clear();
        wrapper.eq("farm_id",farmId);
        wrapper.last("AND PERIOD_DIFF( DATE_FORMAT( NOW( ) , '%Y%m' ) , DATE_FORMAT( add_date, '%Y%m' ) ) =1  ");
        ProdSalesDto lastProdSales = salesMapper.countMonth(wrapper);
        wrapper.clear();
        wrapper.eq("farm_id",farmId);
        wrapper.last("AND PERIOD_DIFF( DATE_FORMAT( NOW( ) , '%Y%m' ) , DATE_FORMAT( add_date, '%Y%m' ) ) =2  ");
        ProdSalesDto lastTwoProdSales = salesMapper.countMonth(wrapper);
        List timeList = new ArrayList();
        List moneyList = new ArrayList();
        List quantityList = new ArrayList();
        List moneyList1 = new ArrayList();
        for (ProdSales prodSales : list) {
            timeList.add(DateUtil.format(prodSales.getAddDate(),"MM-dd"));
            moneyList.add(  prodSales.getTodayMoney());
            quantityList.add(  prodSales.getTodayQuantity());
            moneyList1.add( NumberUtils.format( prodSales.getTodayMoney()));
        }
        wrapper.clear();
        wrapper.eq("farm_id",farmId);
        wrapper.ge("add_date",DateUtil.beginOfMonth(new Date()));
        ProdSalesDto prodSalesDto = salesMapper.countMonth(wrapper);
        wrapper.clear();
        wrapper.ge("add_date",DateUtil.beginOfMonth(new Date()));
        wrapper.orderByDesc("add_date");
        wrapper.eq("farm_id",farmId);
        wrapper.last("limit 1");
        ProdSales one = this.getOne(wrapper);
        if (ObjectUtil.isNotEmpty(one)){
            //本月
            endMap.put("nowMoney", one.getMonthMoney());
            endMap.put("nowQuantity", one.getMonthQuantity());

            endMap.put("timeList",timeList);
            endMap.put("moneyList",moneyList);
            endMap.put("quantityList",quantityList);
            endMap.put("moneyList1",moneyList1);
        }else {
            endMap.put("nowMoney", null);
            endMap.put("nowQuantity", null);

            endMap.put("timeList",null);
            endMap.put("moneyList",null);
            endMap.put("quantityList",null);
            endMap.put("moneyList1",null);
        }
//        //上月
//        wrapper.clear();
//        wrapper.lt("add_date",DateUtil.beginOfMonth(new Date()));
//        wrapper.orderByDesc("add_date");
//        wrapper.eq("farm_id",farmId);
//        wrapper.last("limit 1");
//        ProdSales lastone = this.getOne(wrapper);
//        if (ObjectUtil.isNotEmpty(lastone)){
//            endMap.put("quantity",lastone.getMonthQuantity());
//            endMap.put("quantityPercent", PercentUtil.getGrowth(lastProdSales.getQuantity(),lastTwoProdSales.getQuantity()));
//            endMap.put("money", NumberUtils.format(lastone.getMonthMoney()+""));
//            endMap.put("moneyPercent", PercentUtil.getGrowth(lastProdSales.getMoney(),lastTwoProdSales.getMoney()));
//        }else {
//            endMap.put("quantity",null);
//            endMap.put("quantityPercent", null);
//            endMap.put("money", null);
//            endMap.put("moneyPercent", null);
//        }
        return new Result(ResultCode.SUCCESS,endMap);
    }

    private Map getLastXMonth(Map map, int n) {
        com.huimv.produce.produce.utils.DateUtil du = new com.huimv.produce.produce.utils.DateUtil();
        String startDate = du.getLastXMonthFirstDay(n);
        String endDate = du.getLastXMonthLastDay(n);
        System.out.println("startDate="+startDate);
        System.out.println("lastOneMonthLastDay="+endDate);
        Integer farmId = (Integer)map.get("farmId");
//        Integer type = (Integer)map.get("type");
        List<ProdSales> prodSalesList = salesMapper.selectLastDay(farmId,startDate,endDate);
        System.out.println("prodSalesList.size="+prodSalesList.size());
        for(ProdSales prodSales:prodSalesList){
            System.out.println(""+prodSales.toString());
        }
        Map salesMap = new HashMap();
        if(prodSalesList.size()>0){
            ProdSales prodSales = prodSalesList.get(0);
            salesMap.put("monthMoney",prodSales.getMonthMoney());
            salesMap.put("monthQuantity",prodSales.getMonthQuantity());
        }

        // ================================================================

//        Map endMap = new HashMap();
//        List<ProdSales> list = new ArrayList<>();
//        //本周
//        if (1==type){
//            wrapper.ge("add_date", DateUtil.beginOfWeek(new Date()));
//            wrapper.isNotNull("today_quantity");
//            list = this.list(wrapper);
//        }//自定义
//        else if (3==type) {
//            endDate = endDate+" 23:59:59";
//            startDate = startDate+" 00:00:00";
//            wrapper.between("add_date",startDate,endDate);
//            wrapper.isNotNull("today_quantity");
//            list = this.list(wrapper);
//        }
//        //本月
//        else {
//            wrapper.ge("add_date", DataUill.getTimesMonthmorning());
//            wrapper.isNotNull("today_quantity");
//            list = this.list(wrapper);
//        }
//        wrapper.clear();
//        wrapper.eq("farm_id",farmId);
//        wrapper.last("AND PERIOD_DIFF( DATE_FORMAT( NOW( ) , '%Y%m' ) , DATE_FORMAT( add_date, '%Y%m' ) ) =1  ");
//        ProdSalesDto lastProdSales = salesMapper.countMonth(wrapper);
//        wrapper.clear();
//        wrapper.eq("farm_id",farmId);
//        wrapper.last("AND PERIOD_DIFF( DATE_FORMAT( NOW( ) , '%Y%m' ) , DATE_FORMAT( add_date, '%Y%m' ) ) =2  ");
//        ProdSalesDto lastTwoProdSales = salesMapper.countMonth(wrapper);
        return salesMap;
    }

    @Override
    public Result listClientHome(Integer farmId) {
        ClientHomeDto clientHomeDto = salesMapper.listClientHome(farmId);
        return new Result(ResultCode.SUCCESS,clientHomeDto);
    }


}
