package cn.pluss.platform.controller.home;

import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.controller.BaseNoModelController;
import cn.pluss.platform.entitiy.ShiroUser;
import cn.pluss.platform.entity.MerchantChannelStatus;
import cn.pluss.platform.entity.PlatformData;
import cn.pluss.platform.home.HomeService;
import cn.pluss.platform.merchantChannelStatus.MerchantChannelStatusService;
import cn.pluss.platform.merchantOrder.MerchantOrderService;
import cn.pluss.platform.platformData.PlatformDataService;
import cn.pluss.platform.refundOrder.MerchantRefundOrderService;
import cn.pluss.platform.userApp.UserAppService;
import cn.pluss.platform.util.DateUtils;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.*;

@Controller
@RequestMapping("platform")
public class HomeController extends BaseNoModelController {
    @Autowired
    private MerchantOrderService merchantOrderService;
    @Autowired
    private HomeService homeService;
    @Autowired
    private UserAppService userAppService;

    @Resource
    private MerchantRefundOrderService merchantRefundOrderService;

    @Resource
    private MerchantChannelStatusService merchantChannelStatusService;

    @Resource
    private PlatformDataService platformDataService;

    @GetMapping("/console")
    public String index() {
        return "index/index2";
    }

    @GetMapping("/first")
    public String first() {
        return "index/first";
    }

    @PostMapping("/firstData")
    @ResponseBody
    public Result<?> firstData(){
        Map<String,Object> result = new HashMap<>(20);
//        //获取首页订单统计数据
        Map<String,Object> orderData = merchantOrderService.getPlatformOrderData();

        Map<String,String> transData = merchantOrderService.getPlatformTransData();

//        //统计首页退款数据
        Map<String,Object> refundData = merchantRefundOrderService.getPlatformRefundData();
//        //统计首页其他数据
        Map<String,Object> otherData =  userAppService.getPlatformData();
//        //统计累计金额数据
        Map<String,Object> amtData =  merchantOrderService.getPlatformAmtData();

        //获取昨日这个时候的支付金额
        Date yestNowEndDate = DateUtils.add(Calendar.DAY_OF_MONTH,DateUtils.currentDate(),-1);
        String date = DateUtils.toString(yestNowEndDate,"yyyy-MM-dd");
        String yestStartDate = DateUtils.toString(yestNowEndDate,"yyyy-MM-dd") + " 00:00:00";
        String yestEndDate = DateUtils.toString(yestNowEndDate,"yyyy-MM-dd HH:mm:ss");
        BigDecimal yestDayNowTransAmt = merchantOrderService.getPlatformAmtYestNowData(date,yestStartDate,yestEndDate);
        BigDecimal toDayTransCount = orderData.get("toDayTransCount") == null ? BigDecimal.ZERO :BigDecimal.valueOf((double)orderData.get("toDayTransCount"));
        BigDecimal subtract = toDayTransCount.subtract(yestDayNowTransAmt);
        BigDecimal scale = subtract.divide(yestDayNowTransAmt,4,BigDecimal.ROUND_DOWN).multiply(BigDecimal.valueOf(100)).setScale(2,BigDecimal.ROUND_DOWN);
        result.putAll(orderData);
        result.putAll(refundData);
        result.putAll(otherData);
        result.putAll(amtData);
        result.putAll(transData);
        PlatformData yesterDayData = platformDataService.censusPlatformData();
        result.put("yesterDay",yesterDayData);

        result.put("yestDayNowTransAmt",yestDayNowTransAmt);
        result.put("scale",scale + "%");
        result.put("subtract",Math.abs(subtract.doubleValue()));
        if(scale.doubleValue() < 0){
            result.put("oTypeName","下降");
        }else{
            result.put("oTypeName","上升");
        }
        List<Map<String,Object>> chartTransList = platformDataService.getChartTransCountData();
        List<String> timeList = new ArrayList<>();
        List<String> payAmtList = new ArrayList<>();
        List<String> payNumList = new ArrayList<>();
        for (Map<String,Object> map:chartTransList) {
            timeList.add((String) map.get("days"));
            payAmtList.add(map.get("transCountAmt").toString());
            payNumList.add(map.get("transNum").toString());
        }
        List<String> channelList = new ArrayList<>();
        List<String> channelAmtList = new ArrayList<>();
        List<String> channelNumList = new ArrayList<>();
        List<Map<String,Object>> chartChannelMap = platformDataService.getChartChannelCountData();
        for (Map<String,Object> map:chartChannelMap) {
            channelList.add((String) map.get("channelName"));
            channelAmtList.add(map.get("transCountAmt").toString());
            channelNumList.add(map.get("transNum").toString());
        }
        result.put("timeList",timeList);
        result.put("payAmtList",payAmtList);
        result.put("payNumList",payNumList);
        result.put("channelList",channelList);
        result.put("channelAmtList",channelAmtList);
        result.put("channelNumList",channelNumList);
        return ResultGenerator.genSuccessResult("获取成功",result);
    }

    @PostMapping("/logout")
    @ResponseBody
    public Map<String, Object> login(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        result.put("result", 2);
        // 执行认证登陆
        try {
            result.put("result", 1);
            result.put("code", 0);
            result.put("url", request.getContextPath() + "/login");
        } catch (Exception uae) {
        }
        return result;
    }

    @PostMapping(value = "/showAdmin")
    @ResponseBody
    public Map<String, Object> showAdmin() {
        Map<String, Object> result = new HashMap<>();
        ShiroUser queryCurrentShiroUser = queryCurrentShiroUser();
        String loginName = queryCurrentShiroUser.getLoginName();
        result.put("loginName", loginName);
        return result;
    }

    @PostMapping(value = "/getAuditData")
    @ResponseBody
    public Result<Object> getAuditData() {
        QueryWrapper<MerchantChannelStatus> queryWrapper = new QueryWrapper<MerchantChannelStatus>()
                .eq("status",MerchantChannelStatus.AUDIT_STATUS_EXAMINING)
                .eq("thirdStatus",MerchantChannelStatus.AUDIT_THIRD_STATUS_WAITING)
                .last(" order by id desc").select("id");
        int count = merchantChannelStatusService.count(queryWrapper);
        queryWrapper.last(" order by id desc limit 1");
        Map<String,Object> map = merchantChannelStatusService.getMap(queryWrapper);
        Integer maxId = 0;
        if(map != null){
            maxId = (Integer) map.get("id");
        }

        JSONObject result = new JSONObject(2);
        result.put("id",maxId);
        result.put("count",count);
        return ResultGenerator.genSuccessResult("获取成功",result);
    }
}
