/**
 * Copyright © 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.sys.web;

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

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolationException;
import javax.validation.constraints.NotNull;

import com.jeeplus.modules.sys.entity.CompletionInformationExport;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.google.common.collect.Lists;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.utils.excel.ExportExcel;
import com.jeeplus.common.utils.excel.ImportExcel;
import com.jeeplus.modules.sys.entity.CompletionInformation;
import com.jeeplus.modules.sys.service.CompletionInformationService;

/**
 * ItemListController
 *
 * @author qiuyue
 * @version 2021-12-23
 */
@RestController
@RequestMapping(value = "/sys/completionInformation")
public class CompletionInformationController extends BaseController {

    @Autowired
    private CompletionInformationService completionInformationService;

    @ModelAttribute
    public CompletionInformation get(@RequestParam(required = false) String id) {
        CompletionInformation entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = completionInformationService.get(id);
        }
        if (entity == null) {
            entity = new CompletionInformation();
        }
        return entity;
    }

    /**
     * 项目信息列表数据
     */
    @RequiresPermissions("sys:completionInformation:list")
    @GetMapping("list")
    public AjaxJson list(CompletionInformation completionInformation, HttpServletRequest request, HttpServletResponse response) {
        String itemCode = completionInformation.getItemCode();
        List<String> attrs = new ArrayList<>();
        if (StringUtils.isNotBlank(itemCode)) {
            String[] strArr = itemCode.split("\\s+");
            for (String a :
                    strArr) {
                attrs.add(a);
            }
            completionInformation.setAttrs(attrs);
        }
        Page<CompletionInformation> page = completionInformationService.findPage(new Page<CompletionInformation>(request, response), completionInformation);
        return AjaxJson.success().put("page", page);
    }

    /**
     * 根据Id获取项目信息数据
     */
    @RequiresPermissions(value = {"sys:completionInformation:view", "sys:completionInformation:add", "sys:completionInformation:edit"}, logical = Logical.OR)
    @GetMapping("queryById")
    public AjaxJson queryById(CompletionInformation completionInformation) {
        return AjaxJson.success().put("completionInformation", completionInformation);
    }

    /**
     * 保存项目信息
     */
    @RequiresPermissions(value = {"sys:completionInformation:add", "sys:completionInformation:edit"}, logical = Logical.OR)
    @PostMapping("save")
    public AjaxJson save(CompletionInformation completionInformation, Model model) throws Exception {
        /**
         * 后台hibernate-validation插件校验
         */
        String errMsg = beanValidator(completionInformation);
        if (StringUtils.isNotBlank(errMsg)) {
            return AjaxJson.error(errMsg);
        }
        //新增或编辑表单保存
        completionInformationService.insert(completionInformation);//保存
        return AjaxJson.success("保存项目信息成功");
    }


    /**
     * 保存项目信息
     */
    @RequiresPermissions(value = {"sys:completionInformation:add", "sys:completionInformation:edit"}, logical = Logical.OR)
    @PostMapping("update")
    public AjaxJson update(CompletionInformation completionInformation, Model model) throws Exception {
        /**
         * 后台hibernate-validation插件校验
         */
        String errMsg = beanValidator(completionInformation);
        if (StringUtils.isNotBlank(errMsg)) {
            return AjaxJson.error(errMsg);
        }
        //新增或编辑表单保存
        completionInformationService.update(completionInformation);//保存
        return AjaxJson.success("修改项目信息成功");
    }

    /**
     * 批量删除项目信息
     */
    @RequiresPermissions("sys:completionInformation:del")
    @DeleteMapping("delete")
    public AjaxJson delete(String ids) {
        String idArray[] = ids.split(",");
        //逻辑删除
        for (String id : idArray) {
            completionInformationService.updateDelFlag(id);
        }
        return AjaxJson.success("删除项目信息成功");
    }

    /**
     * 批量判断实际提交日期
     */
    @RequiresPermissions("sys:completionInformation:sub")
    @GetMapping("isSubmissionDate")
    public AjaxJson isSubmissionDate(String ids) {
        String idArray[] = ids.split(",");
        boolean submissionDate = completionInformationService.isSubmissionDate(idArray);
        if (submissionDate) {
            return AjaxJson.success("无项目提交");
        } else {
            return AjaxJson.error("部分项目已提交！");
        }
    }

    /**
     * 批量删除项目信息
     */
    @RequiresPermissions("sys:completionInformation:sub")
    @GetMapping("updateSubmissionDate")
    public AjaxJson updateSubmissionDate(String ids) {
        String idArray[] = ids.split(",");
        int i = completionInformationService.updateSubmissionDate(idArray);
        return AjaxJson.success("已更新" + i + "条记录");
    }


    /**
     * 导出excel文件
     */
    @RequiresPermissions("sys:completionInformation:export")
    @GetMapping("export")
    public AjaxJson exportFile(CompletionInformation completionInformation, HttpServletRequest request, HttpServletResponse response) {
        try {
            String fileName = "项目信息" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
            Page<CompletionInformation> page = completionInformationService.findPage(new Page<CompletionInformation>(request, response, -1), completionInformation);
            new ExportExcel("项目信息", CompletionInformationExport.class).setDataList(page.getList()).write(response, fileName).dispose();
            return null;
        } catch (Exception e) {
            return AjaxJson.error("导出项目信息记录失败！失败信息：" + e.getMessage());
        }
    }

    /**
     * 导入Excel数据
     */
    @RequiresPermissions("sys:completionInformation:import")
    @PostMapping("import")
    public AjaxJson importFile(@RequestParam("file") MultipartFile file, HttpServletResponse response, HttpServletRequest request) {
        try {
            int successNum = 0;
            ImportExcel ei = new ImportExcel(file, 1, 0);
            List<CompletionInformation> list = ei.getDataList(CompletionInformation.class);
            String s = completionInformationService.importVerification(list);
            if (StringUtils.isNotBlank(s)) {
                return AjaxJson.success(s);
            } else {
                //批量插入
                completionInformationService.insertList(list);
                return AjaxJson.success("已成功导入 " + list.size());
            }
        } catch (Exception e) {
            return AjaxJson.error("导入项目信息失败！失败信息：" + e.getMessage());
        }
    }

    /**
     * 下载导入项目信息数据模板
     */
    @RequiresPermissions("sys:completionInformation:import")
    @GetMapping("import/template")
    public AjaxJson importFileTemplate(HttpServletResponse response) {
        try {
            String fileName = "项目信息数据导入模板.xlsx";
            List<CompletionInformation> list = Lists.newArrayList();
            new ExportExcel("项目信息数据", CompletionInformation.class, 1).setDataList(list).write(response, fileName).dispose();
            return null;
        } catch (Exception e) {
            return AjaxJson.error("导入模板下载失败！失败信息：" + e.getMessage());
        }
    }


}