package com.op.mes.controller;

import java.security.Security;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.nacos.client.constant.Constants;
import com.op.common.core.constant.UserConstants;
import com.op.common.core.utils.StringUtils;
import com.op.common.core.utils.poi.ExcelUtil;
import com.op.common.core.utils.uuid.IdUtils;
import com.op.common.core.web.controller.BaseController;
import com.op.common.core.web.domain.AjaxResult;
import com.op.common.core.web.page.TableDataInfo;
import com.op.common.log.annotation.Log;
import com.op.common.log.enums.BusinessType;
import com.op.common.security.annotation.RequiresPermissions;
import com.op.common.security.utils.SecurityUtils;
import com.op.mes.domain.ProProcess;
import com.op.mes.service.IProProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 生产工序Controller
 *
 * @author yinjinlu
 * @date 2022-05-11
 */
@RestController
@RequestMapping("/pro/process")
public class ProProcessController extends BaseController {
    @Autowired
    private IProProcessService proProcessService;

    /**
     * 查询生产工序列表
     */
    @RequiresPermissions("mes:pro:process:list")
    @GetMapping("/list")
    public TableDataInfo list(ProProcess proProcess) {
        startPage();
        List<ProProcess> list = proProcessService.selectProProcessList(proProcess);
        return getDataTable(list);
    }

    /**
     * 查询所有可用工序的清单
     *
     * @return
     */
    @RequiresPermissions("mes:pro:process:list")
    @GetMapping("/listAll")
    public AjaxResult listAll() {
        ProProcess process = new ProProcess();
        process.setEnableFlag("Y");
        List<ProProcess> list = proProcessService.selectProProcessList(process);
        return AjaxResult.success(list);
    }

    /**
     * 导出生产工序列表
     */
    @RequiresPermissions("@ss.hasPermi('mes:pro:process:export')")
    @Log(title = "生产工序", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProProcess proProcess) {
        List<ProProcess> list = proProcessService.selectProProcessList(proProcess);
        ExcelUtil<ProProcess> util = new ExcelUtil<ProProcess>(ProProcess.class);
        util.exportExcel(response, list, "生产工序数据");
    }

    /**
     * 获取生产工序详细信息
     */
    @RequiresPermissions("mes:pro:process:query")
    @GetMapping(value = "/{processId}")
    public AjaxResult getInfo(@PathVariable("processId") String processId) {
        return AjaxResult.success(proProcessService.selectProProcessByProcessId(processId));
    }

    /**
     * 新增生产工序
     */
    @RequiresPermissions("mes:pro:process:add")
    @Log(title = "生产工序", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProProcess proProcess) {
        if (!proProcessService.checkProcessCodeUnique(proProcess)) {
            return AjaxResult.error("工序编码已存在！");
        }
        if (!proProcessService.checkProcessNameUnique(proProcess)) {
            return AjaxResult.error("工序名称已存在！");
        }
        return toAjax(proProcessService.insertProProcess(proProcess));
    }

    /**
     * 修改生产工序
     */
    @RequiresPermissions("mes:pro:process:edit")
    @Log(title = "生产工序", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProProcess proProcess) {
        if (!proProcessService.checkProcessCodeUnique(proProcess)) {
            return AjaxResult.error("工序编码已存在！");
        }
        if (!proProcessService.checkProcessNameUnique(proProcess)) {
            return AjaxResult.error("工序名称已存在！");
        }
        return toAjax(proProcessService.updateProProcess(proProcess));
    }

    /**
     * 删除生产工序
     */
    @RequiresPermissions("mes:pro:process:remove")
    @Log(title = "生产工序", businessType = BusinessType.DELETE)
    @DeleteMapping("/{processIds}")
    public AjaxResult remove(@PathVariable String[] processIds) {
        return toAjax(proProcessService.deleteProProcessByProcessIds(processIds));
    }
}
