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

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huimv.common.utils.Result;
import com.huimv.common.utils.ResultCode;
import com.huimv.produce.produce.entity.ProdSalesDetail;
import com.huimv.produce.produce.mapper.ProdSalesDetailMapper;
import com.huimv.produce.produce.service.IProdSalesDetailService;
import org.springframework.stereotype.Service;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author astupidcoder
 * @since 2021-10-22
 */
@Service
public class ProdSalesDetailServiceImpl extends ServiceImpl<ProdSalesDetailMapper, ProdSalesDetail> implements IProdSalesDetailService {

    @Override
    public Result getSalesDetail(String beginDate, String endDate, Integer farmId) {
        List<ProdSalesDetail> prodSalesDetails ;
        if (StringUtils.isNotBlank(beginDate)){
            prodSalesDetails = this.list(new QueryWrapper<ProdSalesDetail>().between("sell_date", beginDate, endDate));
        }else {
            prodSalesDetails = this.list(new QueryWrapper<ProdSalesDetail>().ge("sell_date", DateUtil.offsetDay(new Date(),-7)));
        }


        List sellDates = new ArrayList();
        List quantitys = new ArrayList();
        for (int i = prodSalesDetails.size()-1; i >= 0; i--){
            ProdSalesDetail sellDate = prodSalesDetails.get(i);
            LocalDate sellDate1 = sellDate.getSellDate();
            sellDates.add((sellDate1+"").substring(5)+getwWeek(sellDate1) );
            quantitys.add(sellDate.getQuantity());
        }

        Map map = new HashMap();
        map.put("sellDates",sellDates);
        map.put("quantitys",quantitys);
        return new Result(ResultCode.SUCCESS,map);
    }

    @Override
    public Result getSalesWeek(Integer farmId) {
        Map map = new HashMap();
        DateTime now = DateUtil.date();
        DateTime nowWeek = DateUtil.beginOfWeek(now);
        List<ProdSalesDetail> nowWeeks = this.list(new QueryWrapper<ProdSalesDetail>().ge("sell_date", nowWeek));

        Integer saleAll = 0;
        Double quantityAll =0.0;
        for (ProdSalesDetail nowWeek1 : nowWeeks) {
            //销售量
            Integer quantity = nowWeek1.getQuantity();
            if (quantity != null){
                saleAll+= quantity;
            }
            //销售额
            Double salesAmount = nowWeek1.getSalesAmount();
            if (salesAmount != null){
                quantityAll += salesAmount;
            }

        }


        DateTime lastWeek = DateUtil.offsetDay(nowWeek, -7);
        List<ProdSalesDetail> lastWeeks = this.list(new QueryWrapper<ProdSalesDetail>().ge("sell_date", lastWeek).lt ("sell_date",nowWeek));
        Double lastQuantityAll =0.0;
        Integer lastSaleAll = 0;
        for (ProdSalesDetail week : lastWeeks) {
            //销售量
            Integer quantity = week.getQuantity();
            if (quantity != null){
                lastSaleAll+= quantity;
            }
            //销售额
            Double salesAmount = week.getSalesAmount();
            if (salesAmount != null){
                lastQuantityAll += salesAmount;
            }
        }

        if (quantityAll >= lastQuantityAll){
            map.put("quantityType" ,true);
            map.put("quantityPercentage", Double.parseDouble(String.format("%.2f", (quantityAll - lastQuantityAll) / lastQuantityAll  * 100)));
        } else  {
            map.put("quantityType" ,false);
            map.put("quantityPercentage", Double.parseDouble(String.format("%.2f",  (lastQuantityAll - quantityAll) / lastQuantityAll  * 100)));
        }

        if (saleAll >= lastSaleAll){
            map.put("saleType" ,true);
            map.put("salePercentage", Double.parseDouble(String.format("%.2f", (float) (saleAll - lastSaleAll) / (float)lastSaleAll  * 100)));
        } else {
            map.put("saleType" ,false);
            map.put("salePercentage", Double.parseDouble(String.format("%.2f", (float) (lastSaleAll - saleAll) / (float)lastSaleAll  * 100) ));
        }


        map.put("quantityAll",quantityAll);
        map.put("saleAll",saleAll);

        return new Result(ResultCode.SUCCESS,map);
    }

    private String getwWeek(LocalDate sellDate1) {
        DayOfWeek dayOfWeek = sellDate1.getDayOfWeek();
        switch (dayOfWeek){
            case MONDAY:
                return "周一";
            case TUESDAY:
                return "周二";
            case WEDNESDAY:
                return "周三";
            case THURSDAY:
                return "周四";
            case FRIDAY:
                return "周五";
            case SATURDAY:
                return "周六";
            default:
                return "周日";

        }
    }



}
