package com.winsun.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.winsun.auth.core.annotion.Permission;
import com.winsun.auth.core.base.controller.BaseController;
import com.winsun.auth.core.common.model.ResponseData;
import com.winsun.auth.core.shiro.ShiroUser;
import com.winsun.bean.Order;
import com.winsun.mapper.OrderMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.formula.functions.Now;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author: chenhaojian
 * @Date: 2021/3/3 10:33
 */
@Slf4j
@RestController
@RequestMapping("/orderConversion")
public class OrderConversionController extends BaseController {

    private static OrderMapper orderMapper;

    // 获取订单通报数据
    // conversionType：
    //       00：移动通报学校
    //       01：移动通报县份
    //       10：宽带通报学校
    //       11：宽带通报学校
//    @RequestMapping(value = "getOrderConversionInfo",method = RequestMethod.POST)
    @Permission(menuname = "获取订单通报数据", value = "getOrderConversionInfo", method = RequestMethod.POST)
    public ResponseData getOrderConversionInfo(@RequestParam("startTime")String startTime,
                                                       @RequestParam("endTime")String endTime,
                                                       @RequestParam("conversionType")String conversionType,
                                                       @RequestParam(name = "pageNo") int pageNo,
                                                       @RequestParam(name = "pageSize") int pageSize){
        ShiroUser user = getShiroUser();
        if (!user.getRoleNames().stream().anyMatch(roleName -> StringUtils.equalsAny(roleName, "超级管理员", "县分管理员（订单）"))) {
            ResponseData.error("无权限查询订单");
        }
        List<Map<String,Object>> list = null;
        Page<Map<String,Object>> page = new Page<>(pageNo, pageSize);
        if("".equals(startTime)&&"".equals(endTime)){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            // 暂时计算2019年6月1号开始数据
            startTime = "2019-06-01";
            endTime = sdf.format(new Date());
        }
        try{
            switch (conversionType){
                case "00":
                    list = orderMapper.orderConversionXX(startTime,endTime);
                    break;
                case "01":
                    list = orderMapper.orderConversionXF(startTime,endTime);
                    break;
                case "10":
                    list = orderMapper.kdOrderConversionXX(startTime,endTime);
                    break;
                case "11":
                    list = orderMapper.kdOrderConversionXF(startTime,endTime);
                    break;
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        page.setRecords(list);
        return ResponseData.success(page,"查询成功！");
    }
}
