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

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.style.column.SimpleColumnWidthStyleStrategy;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.projects.entity.Projects;
import com.gci.boot.modules.projects.service.IProjectsService;
import com.gci.boot.modules.statis.service.BusinessService;
import com.gci.boot.modules.statis.vo.BusinessStatisVo;
import com.gci.boot.modules.statis.vo.StatisParamVO;
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.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 业务板块成本
 * @author 24642
 */
@Api(tags="项目成本统计")
@Slf4j
@RestController
@RequestMapping("/statis/business")
public class BusinessController {

    @Autowired
    private BusinessService businessService;
    @Autowired
    private IProjectsService projectsService;

    @AddSysLog(value = "业务板块成本-分页列表查询")
    @GetMapping(value = "/list")
    @DataPermission(item="A.projectId", sqlIds = {"com.gci.boot.modules.projects.mapper.ProjectsMapper.getAllProjectsAuth"})
    public Result<?> queryPageList(StatisParamVO vo,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        Page<BusinessStatisVo> page = new Page<>(pageNo, pageSize);
        IPage<BusinessStatisVo> pageList = businessService.page(page,vo);
        return Result.ok(pageList);
    }

    /**
     * 导出
     * @return
     */
    @AddSysLog(value = "业务板块成本-导出")
    @GetMapping("/export")
    @DataPermission(item="A.projectId", sqlIds = {"com.gci.boot.modules.projects.mapper.ProjectsMapper.getAllProjectsAuth"})
    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 = businessService.getAll(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("统计期间"));
        //动态列
        List<Projects> list = projectsService.list(new LambdaQueryWrapper<Projects>().orderByAsc(Projects::getCreateTime));
        for (Projects projects : list) {
            headList.add(Collections.singletonList(projects.getProjectName()));
        }
        return headList;
    }
}
