package com.gci.boot.modules.statis.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.style.column.SimpleColumnWidthStyleStrategy;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gci.boot.annotation.permission.DataPermission;
import com.gci.boot.common.log.annotation.AddSysLog;
import com.gci.boot.common.vo.Result;
import com.gci.boot.modules.statis.service.RepairStatisService;
import com.gci.boot.modules.statis.vo.RepairBoardVO;
import com.gci.boot.modules.statis.vo.RepairStatisVO;
import com.gci.boot.modules.statis.vo.StatisParamVO;
import com.gci.boot.modules.system.service.ISysDictService;
import com.gci.boot.modules.system.vo.DictModel;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.*;

@Api(tags="维修统计")
@Slf4j
@RestController
@RequestMapping("/statis/repair")
public class RepairStatisController {

    @Autowired
    private RepairStatisService repairStatisService;
    @Autowired
    private ISysDictService sysDictService;

    /**
     * 分页列表查询
     *
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AddSysLog(value = "维修统计-分页列表查询")
    @GetMapping(value = "/list")
    @DataPermission(item="tri.projectId", sqlIds = {"com.gci.boot.modules.vehicles.mapper.TbRepairInfoMapper.findStatisRepairByDayMonthYear"})
    public Result<?> queryPageList(StatisParamVO vo,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        Page<RepairStatisVO> page = new Page<RepairStatisVO>(pageNo, pageSize);
        IPage<RepairStatisVO> pageList = repairStatisService.getRepairStatisPage(page, vo);
        return Result.ok(pageList);
    }

    /**
     * 导出
     * @return
     */
    @AddSysLog(value = "维修统计-导出")
    @GetMapping("/export")
    @DataPermission(item="tri.projectId", sqlIds = {"com.gci.boot.modules.vehicles.mapper.TbRepairInfoMapper.findStatisRepairAll"})
    public Result<?> export(StatisParamVO vo, HttpServletResponse response){
        try (OutputStream output = response.getOutputStream()){
            String fileName = "维修业务量统计.xlsx";
            try {
                fileName = URLEncoder.encode(fileName, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ";" + "filename*=utf-8''" + fileName);
            List<List<Object>> datas = repairStatisService.getAllRepairStatis(vo);
            EasyExcel.write(output)
                    .head(head())
                    .registerWriteHandler(new SimpleColumnWidthStyleStrategy(20))
                    .sheet("维修业务量统计")
                    .doWrite(datas);

        } catch (IOException e) {
            return Result.error("导出错误！");
        }
        return Result.ok();
    }


    private List<List<String>> head() {
        List<List<String>> headList = new ArrayList<>();
        //固定列
        headList.add(Collections.singletonList("所属项目"));
        headList.add(Collections.singletonList("统计期间"));
        headList.add(Collections.singletonList("车辆种类"));
        headList.add(Collections.singletonList("维修车次"));
        headList.add(Collections.singletonList("维修金额(元)"));
        headList.add(Collections.singletonList("平均金额(车/元)"));
        //动态列
        List<DictModel> modelList = sysDictService.queryDictItemsByCode("repair_class");
        modelList.sort(Comparator.comparing(DictModel::getValue));
        for (DictModel dictModel : modelList) {
            headList.add(Collections.singletonList(dictModel.getText()));
        }
        return headList;
    }

    /**
     * 数据看板----维修情况
     * @param startTime
     * @param endTime
     * @return
     */
    @GetMapping(value = "/getRepairInfo")
    public Result<?> getRepairInfo(String startTime, String endTime) {
        Date startParam = null;
        Date endParam = null;
        if(StrUtil.isNotBlank(startTime)){
            startParam = DateUtil.parse(startTime);
        }
        if(StrUtil.isNotBlank(endTime)){
            endParam = DateUtil.parse(endTime);
        }

        List<RepairBoardVO> getRepairInfo = repairStatisService.getRepairInfo(startParam,endParam);
        return Result.ok(getRepairInfo);
    }
}
