package com.ruoyi.system.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.ActSuppliesRole;
import com.ruoyi.system.domain.ActSuppliesTemplate;
import com.ruoyi.system.service.ActSuppliesTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 物资管理Controller
 *
 * @author ruoyi
 * @date
 */
@RestController
@RequestMapping("/SuppliesTemplate/SuppliesTemplate")
public class ActSuppliesTemplateController extends BaseController {

    @Autowired
    private ActSuppliesTemplateService actSuppliesTemplateService;

    /**
     * 查询Excel模板列表
     */
    @PreAuthorize("@ss.hasPermi('SuppliesTemplate:SuppliesTemplate:list')")
    @GetMapping("/list")
    public TableDataInfo list(ActSuppliesTemplate sysSupplies) {

        startPage();
        List<ActSuppliesTemplate> list = actSuppliesTemplateService.selectSuppliesTemplateList(sysSupplies);
        return getDataTable(list);

    }

    /**
     * 查询导入规则列表
     */
    @PreAuthorize("@ss.hasPermi('SuppliesTemplate:SuppliesTemplate:lists')")
    @GetMapping("/lists/{id}")
    public TableDataInfo lists(@PathVariable("id") Integer id)
    {
        startPage();
        List<ActSuppliesRole> list = actSuppliesTemplateService.selectActSuppliesRoleList(id);
        return getDataTable(list);
    }

    /**
     * 修改导入规则
     */
    @PreAuthorize("@ss.hasPermi('SuppliesTemplate:SuppliesTemplate:edit')")
    @Log(title = "导入规则", businessType = BusinessType.UPDATE)
    @PutMapping("/edit/{id}")
    public AjaxResult edit(@RequestBody  ActSuppliesRole actSuppliesRole)
    {

        SysUser user = SecurityUtils.getLoginUser().getUser();
        actSuppliesRole.setUpdateBy(user.getUserName());
        return toAjax(actSuppliesTemplateService.updateActSuppliesRole(actSuppliesRole));
    }

    /**
     * 导出Excel模板列表
     */
    @PreAuthorize("@ss.hasPermi('SuppliesTemplate:SuppliesTemplate:export')")
    @Log(title = "Excel模板", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ActSuppliesTemplate sysSupplies) {
        List<ActSuppliesTemplate> list = actSuppliesTemplateService.selectSuppliesTemplateList(sysSupplies);
        ExcelUtil<ActSuppliesTemplate> util = new ExcelUtil<ActSuppliesTemplate>(ActSuppliesTemplate.class);
        util.exportExcel(list, "Excel模板数据");
    }

    /**
     * 获取Excel模板详细信息
     */
    @PreAuthorize("@ss.hasPermi('SuppliesTemplate:SuppliesTemplate:query')")
    @GetMapping(value = "/{id}")
    public TableDataInfo getInfo(@PathVariable("id") Long id) {
        return getDataTable(actSuppliesTemplateService.selectSuppliesTemplateById(id));
    }

    /**
     * 新增Excel模板
     */
    @PreAuthorize("@ss.hasPermi('SuppliesTemplate:SuppliesTemplate:add')")
    @Log(title = "Excel模板", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ActSuppliesTemplate sysSupplies) {
        int i = actSuppliesTemplateService.insertSuppliesTemplate(sysSupplies);
        return toAjax(i);
    }

    /**
     *获取excel模板内容
     */
    @PreAuthorize("@ss.hasPermi('SuppliesTemplate:SuppliesTemplate:listId')")
    @GetMapping("/listId/{id}")
    public TableDataInfo listId(@PathVariable("id") Long id) {
        List<ActSuppliesTemplate> sysSupplies = actSuppliesTemplateService.saveTemplate(id);
        return getDataTable(sysSupplies);
    }

    /**
     * 修改Excel模板
     */
    @PreAuthorize("@ss.hasPermi('SuppliesTemplate:SuppliesTemplate:edit')")
    @Log(title = "Excel模板", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ActSuppliesTemplate sysSupplies) {
        return toAjax(actSuppliesTemplateService.updateSuppliesTemplate(sysSupplies));
    }

    /**
     * 发布Excel模板
     */
    @PreAuthorize("@ss.hasPermi('SuppliesTemplate:SuppliesTemplate:releaseStatus')")
    @Log(title = "Excel模板", businessType = BusinessType.UPDATE)
    @GetMapping("/releaseStatus/{ids}")
    public AjaxResult releaseStatus(@PathVariable Long[] ids) {
        return toAjax(actSuppliesTemplateService.releaseStatus(ids));
    }

    /**
     * 下架Excel模板
     */
    @PreAuthorize("@ss.hasPermi('SuppliesTemplate:SuppliesTemplate:offShelfStatus')")
    @Log(title = "Excel模板", businessType = BusinessType.UPDATE)
    @GetMapping("/offShelfStatus/{ids}")
    public AjaxResult offShelfStatus(@PathVariable Long[] ids) {
        return toAjax(actSuppliesTemplateService.offShelfStatus(ids));
    }

    /**
     * 下架Excel模板
     */
    @PreAuthorize("@ss.hasPermi('SuppliesTemplate:SuppliesTemplate:copyTemplate')")
    @Log(title = "Excel模板", businessType = BusinessType.UPDATE)
    @GetMapping("/copyTemplate/{id}")
    public AjaxResult copyTemplate(@PathVariable Long id) {
        String name = actSuppliesTemplateService.copyTemplate(id);
        return AjaxResult.success("200",name);
    }

    /**
     * 删除Excel模板
     */
    @PreAuthorize("@ss.hasPermi('SuppliesTemplate:SuppliesTemplate:remove')")
    @Log(title = "Excel模板", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {

        List<String> list = actSuppliesTemplateService.deleteSuppliesTemplateByIds(ids);

        if (list.size()>0){
            return AjaxResult.success("312",list.toArray());
        }
        return toAjax(1);
    }

    /**
     * 判断模板名称是否重复
     */
    @PreAuthorize("@ss.hasPermi('SuppliesTemplate:SuppliesTemplate:ifName')")
    @GetMapping("/ifName/{name}")
    public AjaxResult ifName(@PathVariable("name") String name)
    {
        List<String> find = actSuppliesTemplateService.findName();

        if (find.contains(name)){
            return AjaxResult.success("上传文件名重复，请修改后上传！","存在重复");
        }
        return AjaxResult.success("验证通过","验证通过");
    }

    /**
     * 启用物料总分类管理
     */
    @PreAuthorize("@ss.hasPermi('SuppliesTemplate:SuppliesTemplate:enable')")
    @GetMapping("/enable/{id}")
    public AjaxResult setEnable(@PathVariable("id") Long id)
    {
        actSuppliesTemplateService.setEnable(id);
        return AjaxResult.success();
    }

    /**
     * 禁用物料总分类管理
     */
    @PreAuthorize("@ss.hasPermi('SuppliesTemplate:SuppliesTemplate:Disable')")
    @GetMapping("/disable/{id}")
    public AjaxResult setDisable(@PathVariable("id") Long id)
    {
//        if(actSuppliesTemplateService.find(id).size()>0){
//            return AjaxResult.success("该物料下存在关联物料，请先删除关联物料！！！","操作失败");
//        }
        actSuppliesTemplateService.setDisable(id);
        return AjaxResult.success();
    }
}