package com.bootdo.datas.controller;

import com.alibaba.fastjson.JSONArray;
import com.bootdo.common.annotation.Log;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.datas.domain.DataDO;
import com.bootdo.datas.domain.GyDataExcelDO;
import com.bootdo.datas.dto.GyDataImportDTO;
import com.bootdo.datas.service.DataService;
import com.bootdo.datas.service.GyDataService;
import com.bootdo.datas.tools.ExcelUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 重要数据表
 *
 * @author admin
 * @email admin@163.com
 * @date 2022-03-06 10:37:54
 */

@Controller
@RequestMapping("/datas/unionVerify")
public class DataUnionController {

    private static Logger log = LoggerFactory.getLogger(DataUnionController.class);

    @Autowired
    private DataService dataService;
    @Autowired
    private GyDataService gyDataService;

    @GetMapping()
    @Log("访问联合审查")
    @RequiresPermissions("datas:unionVerify:data")
    String Data() {

        return "datas/unionVerify/data";
    }

    @GetMapping("dataInfo")
    @RequiresPermissions("datas:unionVerify:data")
    String dataInfo(String type, Model model) {
        log.info("dataLevel:" + type);
        model.addAttribute("dataLevel", type);
        return "datas/unionVerify/dataInfo";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("datas:unionVerify:data")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        // 查询列表数据
        Query query = new Query(params);
        List<GyDataImportDTO> dataList = gyDataService.listUnion(query);
        int total = gyDataService.countUnion(query);
        PageUtils pageUtils = new PageUtils(dataList, total);
        return pageUtils;
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("datas:unionVerify:edit")
    String edit(@PathVariable("id") Long id, Model model) {
        GyDataImportDTO data = gyDataService.get(id);
        model.addAttribute("data", data);
        return "datas/unionVerify/edit";
    }

    /**
     * @Description: 列表页面 审核通过
     * @Author: wangp
     * @Date: 2022/3/6 16:35
     * @param id
     * @Return: R
     */
    @PostMapping("/verify")
    @ResponseBody
    @RequiresPermissions("datas:unionVerify:verify")
    public R verify(Long id) {
        log.info("审核通过 id:" + id);
        GyDataImportDTO data = new GyDataImportDTO();
        // 0:正常 1:删除 2:已上报 3:通过审核 4:驳回
        data.setId(id);
        data.setDataStatus("3");
        gyDataService.updateStatus(data);
        return R.ok();
    }

    /**
     * @Description: 批量审核
     * @Author: wangp
     * @Date: 2022/3/6 16:35
     * @param ids
     * @Return: R
     */
    @PostMapping("/batchVerify")
    @ResponseBody
    @RequiresPermissions("datas:unionVerify:verify")
    public R batchVerify(@RequestParam("ids[]") Long[] ids) {
        log.info("批量审核通过 ids:" + ids.toString());
        for (Long id : ids) {
            log.info("审核通过 id:" + id);
            GyDataImportDTO data = new GyDataImportDTO();
            // 判断是否已审核过
            data = gyDataService.get(id);
            log.info("status:" + data.getDataStatus());
            if ("5".equals(data.getDataStatus())) {
                // 0:正常 1:删除 2:已上报 3:通过审核 4:驳回 5:联合审批 6:终审
                data.setId(id);
                data.setDataStatus("3");
                gyDataService.updateStatus(data);
            }
        }
        return R.ok();
    }

    /**
     * @Description: 列表页面 驳回
     * @Author: wangp
     * @Date: 2022/3/6 16:35
     * @param id
     * @Return: R
     */
    @PostMapping("/rebut")
    @ResponseBody
    @RequiresPermissions("datas:unionVerify:rebut")
    public R rebut(Long id) {
        log.info("驳回  id:" + id);
        GyDataImportDTO data = new GyDataImportDTO();
        // 0:正常 1:删除 2:已上报 3:通过审核 4:驳回
        data.setId(id);
        data.setDataStatus("4");
        gyDataService.updateStatus(data);
        return R.ok();
    }

    /**
     * @Description: 批量驳回
     * @Author: wangp
     * @Date: 2022/3/3 14:10
     * @param ids
     * @Return: R
     */
    @PostMapping("/batchRebut")
    @ResponseBody
    @RequiresPermissions("datas:unionVerify:rebut")
    public R batchRebut(@RequestParam("ids[]") Long[] ids) {
        log.info("审核通过 备案主键 ids:" + ids);
        for (Long id : ids) {
            log.info("审核通过 id:" + id);
            GyDataImportDTO data = new GyDataImportDTO();
            // 判断是否已审核过
            data = gyDataService.get(id);
            log.info("status:" + data.getDataStatus());
            if ("5".equals(data.getDataStatus())) {
                // 0:正常 1:删除 2:已上报 3:通过审核 4:驳回 5:联合审批 6:终审
                data.setId(id);
                data.setDataStatus("4");
                gyDataService.updateStatus(data);
            }
        }
        return R.ok();
    }

    /**
     * @Description: 批量导出
     * @Author: wangp
     * @Date: 2022/3/16 10:46
     * @param idValue
     * @Return: R
     */
    @GetMapping("/batchExports")
    public void batchExports(HttpServletResponse responses, String idValue) {
        log.info("批量导出 备案主键 idvalue:" + idValue);
        List<GyDataExcelDO> list = new ArrayList<>();
        JSONArray json = (JSONArray)JSONArray.parse(idValue);
        for (int i = 0; i < json.size(); i++) {
            String id = String.valueOf(json.get(i));
            log.info("批量导出 id:" + json.get(i));
            GyDataExcelDO data = new GyDataExcelDO();
            data = gyDataService.getDataExcel(Long.parseLong(id));
            data.setNo(i + 1); //序号
            list.add(data);
        }
        log.info("list size:" + list.size());
        // 导出xlsx操作
        try {
            ExcelUtils.export(responses, GyDataExcelDO.class, list, "数据目录备案导出信息");
        } catch (Exception e) {
            log.error("export error", e);
        }
    }

}
