package cn.feast.coding.controller.order;

import javax.servlet.http.HttpServletRequest;

import cn.feast.coding.bean.ColumnChartData;
import cn.feast.coding.bean.PieChartData;
import cn.feast.coding.controller.BaseController;
import cn.feast.coding.model.order.MonthStatis;
import cn.feast.coding.model.order.Order;
import cn.feast.coding.model.order.OrderStatis;
import cn.feast.coding.service.order.OrderService;
import cn.feast.coding.tools.JsonResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;


@Controller
@RequestMapping("/order")
public class OrderController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(OrderController.class);
    @Autowired
    private OrderService orderService;

    private static final String FEASTCODING_SHOP = "feastcoding";
    private static final String FEASTCODE_SHOP = "feastcode";

    @RequestMapping(value = "/list")
    public String list(Order order, Integer curPage, Integer pageSize, ModelMap model, HttpServletRequest request)
    {
        PageInfo<Order> pageInfo = orderService.queryPage(order, pageNum(curPage), pageSize(request, pageSize));
        model.put("pages", pageInfo);
        model.put("order", order);
        return "order/order_list";
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(ModelMap model)
    {
        return "order/order_add";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String add(Order order, HttpServletRequest request, ModelMap model)
    {
        order.setOrderStatus(0);
        orderService.insert(order);
        model.put("flag", 1);
        return "order/order_add";
    }


    @RequestMapping(value = "/update", method = RequestMethod.GET)
    public String update(Order order, ModelMap model)
    {
        order = orderService.selectByPrimaryKey(order.getId());
        model.put("order", order);
        return "order/order_edit";
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(Order order, HttpServletRequest request, ModelMap model)
    {
        orderService.update(order);
        model.put("flag", 1);
        return "order/order_edit";
    }

    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    @ResponseBody
    public void delete(Order order)
    {
        orderService.deleteByPrimaryKey(order.getId());
    }

    @RequestMapping(value = "/finish", method = RequestMethod.GET)
    @ResponseBody
    public JsonResult finish(Order order)
    {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setState(JsonResult.SUCCESS);
        try {
            order = orderService.selectByPrimaryKey(order.getId());
        } catch (NullPointerException e) {
            jsonResult.setState(JsonResult.ERROR);
            jsonResult.setMessage("order不存在");
        }
        order.setOrderStatus(1);
        orderService.update(order);
        jsonResult.setData(order);
        return jsonResult;
    }

    @RequestMapping(value = "/statis/page", method = RequestMethod.GET)
    public String statisPage()
    {
        return "order/order_charts";
    }

    @RequestMapping(value = "/moneyPieChart", method = RequestMethod.GET)
    @ResponseBody
    public JsonResult moneyPieChart(@RequestParam(value = "year", required = false) String year)
    {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setState(JsonResult.SUCCESS);
        try {
            List<OrderStatis> list = orderService.statisOrderMoney(year);
            PieChartData pieChartData = new PieChartData("订单统计", list);
            List<PieChartData> dataList = new ArrayList<>();
            dataList.add(pieChartData);
            jsonResult.setData(dataList);
            jsonResult.setMessage(year == null ? "全部" : year+"年");
        } catch (Exception e) {
            logger.error("服务器错误："+e.getMessage(), e);
            jsonResult.setState(JsonResult.ERROR);
            jsonResult.setMessage("数据获取错误");
        }
        return jsonResult;
    }

    @RequestMapping(value = "/countPieChart", method = RequestMethod.GET)
    @ResponseBody
    public JsonResult countPieChart(@RequestParam(value = "year", required = false) String year)
    {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setState(JsonResult.SUCCESS);
        try {
            List<OrderStatis> list = orderService.statisOrderCount(year);
            PieChartData pieChartData = new PieChartData("订单统计", list);
            List<PieChartData> dataList = new ArrayList<>();
            dataList.add(pieChartData);
            jsonResult.setData(dataList);
            jsonResult.setMessage(year == null ? "全部" : year+"年");
        } catch (Exception e) {
            logger.error("服务器错误："+e.getMessage(), e);
            jsonResult.setState(JsonResult.ERROR);
            jsonResult.setMessage("数据获取错误");
        }
        return jsonResult;
    }

    @RequestMapping(value = "/statis/month", method = RequestMethod.GET)
    public String statisMonthPage()
    {
        return "order/order_month_chart";
    }

    @RequestMapping(value = "/moneyCoulumnChart", method = RequestMethod.GET)
    @ResponseBody
    public JsonResult moneyCoulumnChart(@RequestParam(value = "year", defaultValue = "2020") String year)
    {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setState(JsonResult.SUCCESS);
        try {
            List<ColumnChartData> list = new ArrayList<>();
            list.add(new ColumnChartData(FEASTCODING_SHOP, orderService.statisOrderMonthMoney(year, FEASTCODING_SHOP)));
            list.add(new ColumnChartData(FEASTCODE_SHOP, orderService.statisOrderMonthMoney(year, FEASTCODE_SHOP)));
            jsonResult.setData(list);
            jsonResult.setMessage(year);
        }catch (Exception e){
            logger.error("服务器错误："+e.getMessage(), e);
            jsonResult.setState(JsonResult.ERROR);
            jsonResult.setMessage("数据获取错误");
        }
        return jsonResult;
    }

    @RequestMapping(value = "/countCoulumnChart", method = RequestMethod.GET)
    @ResponseBody
    public JsonResult countCoulumnChart(@RequestParam(value = "year", defaultValue = "2020") String year)
    {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setState(JsonResult.SUCCESS);
        try {
            List<ColumnChartData> list = new ArrayList<>();
            list.add(new ColumnChartData(FEASTCODING_SHOP, orderService.statisOrderMonthCount(year, FEASTCODING_SHOP)));
            list.add(new ColumnChartData(FEASTCODE_SHOP, orderService.statisOrderMonthCount(year, FEASTCODE_SHOP)));
            jsonResult.setData(list);
            jsonResult.setMessage(year);
        }catch (Exception e){
            logger.error("服务器错误："+e.getMessage(), e);
            jsonResult.setState(JsonResult.ERROR);
            jsonResult.setMessage("数据获取错误");
        }
        return jsonResult;
    }

}
