package com.chinacreator.video.restful.controller;

import com.alibaba.fastjson.JSONObject;
import com.chinacreator.video.bean.OrderInfo;
import com.chinacreator.video.exception.BusinessException;
import com.chinacreator.video.restful.service.QueryPrmInfoService;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

@RestController
public class QueryPrmInfoController {

    @Autowired
    private QueryPrmInfoService queryPrmInfoService;


    @RequestMapping("/queryPrmInfo")
    public String queryPrmInfo(@RequestBody String params) throws Exception {
        if (StringUtils.isEmpty(params)) {
            throw new BusinessException("9001", "参数不能为空");
        }
        OrderInfo orderInfo = JSONObject.parseObject(params, OrderInfo.class);
        orderInfo.setCpid("prmInfo");
        System.out.println("prm========="+JSONObject.toJSONString(orderInfo));
        queryPrmInfoService.busiCheck(orderInfo);
        SimpleDateFormat sdfDay = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdfMonth = new SimpleDateFormat("yyyy-MM");
        String ordertime = orderInfo.getOrdertime();
        Date date = sdfDay.parse(ordertime);
        if (date.getTime() > ((new Date()).getTime())) {
            throw new BusinessException("9001", "传输时间不能大于当前时间");
        }
        String spid = orderInfo.getSpid();
        if (StringUtils.isEmpty(spid)){
            throw new BusinessException("9001","spid不能为空");
        }

        Map<String, Object> res=new HashMap<String, Object>();
        String resultCode = "0";
        String errorMsg = "";
       String name=queryPrmInfoService.getSpName(spid);
        Map<String, Object> map = new LinkedHashMap<String, Object>();
        try {
            //上月
            String month = sdfMonth.format(DateUtils.addMonths(date,-1));
            //昨天日期
            Date lastDate = DateUtils.addDays(date, -1);
            String lastDay = sdfDay.format(lastDate);
            //获取昨日增长
            String addDayNub = queryPrmInfoService.getNub(lastDay, "0", "0",spid);
            map.put("addDay", addDayNub);
            //获取昨日净增
            String netDayNub = queryPrmInfoService.getNub(lastDay, "0", "1",spid);
            map.put("netDay", netDayNub);
            //获取当月增长
            String addMonth = queryPrmInfoService.getNub(lastDay, "1", "0",spid);
            map.put("addMonth", addMonth);
            //获取上个月净增
            String lastMonth = queryPrmInfoService.getNub(month, "1", "1",spid);
            map.put("netLastMonth", lastMonth);
            //获取当月金额
            String monthMoney = queryPrmInfoService.getNub(lastDay, "1", "4",spid);
            map.put("monthMoney", monthMoney);
            //获取用户存量
            String total = queryPrmInfoService.getNub(lastDay, "2", "0",spid);
            map.put("total", total);
            //获取2元产品昨日订购量
            String orderDay = queryPrmInfoService.getNub(lastDay, "0", "3",spid);
            map.put("orderDay", orderDay);
            //获取2元产品当月订购量
            String orderMonth = queryPrmInfoService.getNub(lastDay, "1", "3",spid);
            map.put("orderMonth", orderMonth);
            //获取2元产品当前订购量
            String totalOrder = queryPrmInfoService.getNub(lastDay, "2", "3",spid);
            map.put("totalOrder", totalOrder);
        } catch (Exception e) {
            resultCode="8000";
            errorMsg=e.getMessage();
            e.printStackTrace();
        }finally {
            res.put("resultCode",resultCode);
            res.put("errorMsg",errorMsg);
            res.put("spname",name);
            res.put("spid",spid);
            res.put("data",map);
        }
        return JSONObject.toJSONString(res);
    }

}
