package com.huimv.admin.service.impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.huimv.admin.common.utils.DataUill;
import com.huimv.admin.common.utils.Result;
import com.huimv.admin.common.utils.ResultCode;
import com.huimv.admin.entity.ProdSale;
import com.huimv.admin.entity.vo.ProdSaleVo;
import com.huimv.admin.mapper.ProdSaleMapper;
import com.huimv.admin.service.IProdSaleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author author
 * @since 2023-02-22
 */
@Service
public class ProdSaleServiceImpl extends ServiceImpl<ProdSaleMapper, ProdSale> implements IProdSaleService {

    @Autowired
    private ProdSaleMapper saleMapper;

    @Override
    public Result list(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
        String farmId = paramsMap.get("farmId");
        String displayType = paramsMap.get("displayType");
        String startTime = paramsMap.get("startTime");
        String endTime = paramsMap.get("endTime");

        if (StringUtils.isBlank(displayType)){
            displayType = "0";
        }

        Integer moneyCount = 0;
        Integer quantityCount = 0;
        Integer moneyCount1 = 0;
        Integer quantityCount1 = 0;


        ProdSaleVo prodSaleVo = new ProdSaleVo();
        QueryWrapper<ProdSale> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("display_type",Integer.parseInt(displayType));
        queryWrapper.eq("farm_id", farmId);
        if (StringUtils.isBlank(startTime) || StringUtils.isBlank(endTime)) {
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - 7);
            queryWrapper.ge("creat_time", calendar.getTime()).orderByAsc("creat_time");
            List<ProdSale> prodSales = saleMapper.selectList(queryWrapper);
            if (prodSales.size() == 0) {
                prodSaleVo.setMoneyRate("0");
                prodSaleVo.setQuantityRate("0");
                prodSaleVo.setSales(null);
            } else {
                prodSaleVo.setSales(prodSales);
            }
        } else {
            startTime = startTime + " 00:00:00";
            endTime = endTime + " 23:59:59";
            queryWrapper.between("creat_time", startTime, endTime).orderByAsc("creat_time");
            List<ProdSale> prodSales = saleMapper.selectList(queryWrapper);
            if (prodSales.size() == 0) {
                prodSaleVo.setMoneyRate("0");
                prodSaleVo.setQuantityRate("0");
                prodSaleVo.setSales(null);
            } else {
                prodSaleVo.setSales(prodSales);
            }
        }

        //本月
        Date timesMonthmorning = DataUill.getTimesMonthmorning();
        QueryWrapper<ProdSale> saleQueryWrapper = new QueryWrapper<>();
        saleQueryWrapper.eq("display_type",Integer.parseInt(displayType));
        saleQueryWrapper.ge("creat_time", timesMonthmorning).eq("farm_id", farmId);
        List<ProdSale> prodSales = saleMapper.selectList(saleQueryWrapper);
        for (ProdSale prodSale : prodSales) {
            moneyCount = moneyCount + Integer.parseInt(prodSale.getSaleMoney());
            quantityCount = quantityCount + prodSale.getSaleQuantity();
        }


        //上个月
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //格式化时间
        //获取上个月的第一天
        Calendar cal_1 = Calendar.getInstance();//获取当前日期
        cal_1.add(Calendar.MONTH, -1);
        cal_1.set(Calendar.DAY_OF_MONTH, 1);//设置为1号
        cal_1.set(Calendar.HOUR_OF_DAY, 0);
        cal_1.set(Calendar.MINUTE, 0);
        cal_1.set(Calendar.SECOND, 0);
        String firstDay = format.format(cal_1.getTime());

        //获取上个月的最后一天
        Calendar cal_2 = Calendar.getInstance();
        cal_2.set(Calendar.DAY_OF_MONTH, 0);//设置为1号,当前日期既为本月第一天
        cal_2.set(Calendar.HOUR_OF_DAY, 23);
        cal_2.set(Calendar.MINUTE, 59);
        cal_2.set(Calendar.SECOND, 59);
        String lastDay = format.format(cal_2.getTime());
        QueryWrapper<ProdSale> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("display_type",Integer.parseInt(displayType));
        queryWrapper1.between("creat_time", firstDay, lastDay);
        List<ProdSale> prodSales1 = saleMapper.selectList(queryWrapper1);
        for (ProdSale prodSale : prodSales1) {
            moneyCount1 = moneyCount1 + Integer.parseInt(prodSale.getSaleMoney());
            quantityCount1 = quantityCount1 + prodSale.getSaleQuantity();
        }

        prodSaleVo.setMoneyCountLast(moneyCount1.toString());
        prodSaleVo.setQuantityCountLast(quantityCount1.toString());
        prodSaleVo.setMoneyCount(moneyCount.toString());//本月销售数量
        prodSaleVo.setQuantityCount(quantityCount.toString());//本月销售额
        double a = ((moneyCount.doubleValue() - moneyCount1.doubleValue()) / moneyCount1.doubleValue())*100;
        double b = (quantityCount.doubleValue() - quantityCount1.doubleValue())/ quantityCount1.doubleValue()*100;
        DecimalFormat df = new DecimalFormat("#.00");
        prodSaleVo.setMoneyRate(df.format(a));
        prodSaleVo.setQuantityRate(df.format(b));
        return new Result(ResultCode.SUCCESS, prodSaleVo);
    }

    @Override
    public Result add(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {


        return new Result(ResultCode.SUCCESS,"添加成功");
    }

    @Override
    public Result listScreen(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
        String farmId = paramsMap.get("farmId");
        String type = paramsMap.get("type");
        String displayType = paramsMap.get("displayType");
        if (StringUtils.isBlank(displayType)){
            displayType = "0";
        }
        QueryWrapper<ProdSale> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("display_type",Integer.parseInt(displayType));
        queryWrapper.eq("farm_id", farmId);
        if (type == null || type == "") {
            type = "1";
        }

        if ("1".equals(type)) {
            queryWrapper.ge("creat_time", DateUtil.offsetMonth(DateUtil.beginOfMonth(new Date()),-6));
            List<ProdSale> prodSales = saleMapper.listMonth(queryWrapper);
            return new Result(ResultCode.SUCCESS,prodSales);
        } else {
            queryWrapper.ge("creat_time", DateUtil.offsetMonth(DateUtil.beginOfMonth(new Date()),-60));
            List<ProdSale> prodSales = saleMapper.listYear(queryWrapper);
            return new Result(ResultCode.SUCCESS,prodSales);
        }

    }
}
