package com.dxhy.manage.controller;

import com.alibaba.fastjson.JSONObject;
import com.dxhy.common.controller.AbstractController;
import com.dxhy.common.utils.PageUtils;
import com.dxhy.common.utils.R;
import com.dxhy.manage.entity.SysDeptEntity;
import com.dxhy.manage.service.CheckStatService;
import com.dxhy.manage.service.SellersStatService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.Base64;
import java.util.List;
import java.util.Map;

/**
 * @author jiaohongyang
 */
@SuppressWarnings("AlibabaUndefineMagicConstant")
@RestController
@RequestMapping("/manage/checkStat")
@Slf4j
public class CheckStatController extends AbstractController {

    @Resource
    private CheckStatService checkStatService;

    /**
     * 返回查验集团信息
     */
    @RequestMapping("/blocList")
    public ResponseEntity<?> list() {
        List<SysDeptEntity> deptList = checkStatService.queryTaxnoList();
        return ResponseEntity.ok(R.ok().put("data", deptList));
    }

    /**
     * 查询结果
     *
     * @param params
     * @return
     */
    @RequestMapping("/getStatResult")
    public ResponseEntity<?> getTaxnoManage(@RequestBody Map<String, String> params) {
        // 根据页面参数不同返回不同结果内容
        String timeType = params.get("timeType");
        if ("0".equals(timeType)) {
            JSONObject obj = checkStatService.getDayTitle();
            PageUtils pageUtils = checkStatService.queryDayPage(params);
            obj.put("datalist", pageUtils.getList());
            obj.put("total", pageUtils.getTotalCount());
            String jsonString = null;
            if (obj != null) {
                try {
                    jsonString = Base64.getEncoder().encodeToString(obj.toJSONString().getBytes("UTF-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
            return ResponseEntity.ok(R.ok().put("data", jsonString));
        } else if ("1".equals(timeType)) {
            JSONObject obj = checkStatService.getYearTitle();
            PageUtils pageUtils = checkStatService.queryYearPage(params);
            obj.put("datalist", pageUtils.getList());
            obj.put("total", pageUtils.getTotalCount());
            String jsonString = null;
            if (obj != null) {
                try {
                    jsonString = Base64.getEncoder().encodeToString(obj.toJSONString().getBytes("UTF-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
            return ResponseEntity.ok(R.ok().put("data", jsonString));
        } else if ("2".equals(timeType)) {
            JSONObject obj = checkStatService.getMonthTitle();
            PageUtils pageUtils = checkStatService.queryMonthPage(params);
            obj.put("datalist", pageUtils.getList());
            obj.put("total", pageUtils.getTotalCount());
            String jsonString = null;
            if (obj != null) {
                try {
                    jsonString = Base64.getEncoder().encodeToString(obj.toJSONString().getBytes("UTF-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
            return ResponseEntity.ok(R.ok().put("data", jsonString));
        }
        return ResponseEntity.ok(R.ok().put("data", null));
    }

    /**
     * 查询结果
     *
     * @param params
     * @return
     */
    @RequestMapping("/exportCheckStatResult")
    public void exportStatResult(@RequestBody Map<String, String> params, HttpServletResponse response) {

        checkStatService.exportExcel(params, response);
    }
}
