package cn.ezhizao.project.business.process.controller;

import cn.ezhizao.framework.aspectj.lang.annotation.Log;
import cn.ezhizao.framework.aspectj.lang.enums.BusinessType;
import cn.ezhizao.framework.web.controller.BaseController;
import cn.ezhizao.framework.web.domain.AjaxResult;
import cn.ezhizao.framework.web.page.TableDataInfo;
import cn.ezhizao.project.business.process.domain.BizProcess;
import cn.ezhizao.project.business.process.service.IBizProcessService;
import cn.ezhizao.project.business.technologicalProcessDetail.domain.BizTechnologicalProcessDetail;
import cn.ezhizao.project.business.technologicalProcessDetail.service.IBizTechnologicalProcessDetailService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 标准工序Controller
 *
 * @author ezhizao
 * @date 2023-11-13
 */
@RestController
@RequestMapping("/business/process")
public class BizProcessController extends BaseController {

    @Resource
    private IBizProcessService bizProcessService;
    @Resource
    private IBizTechnologicalProcessDetailService bizTechnologicalProcessDetailService;
    @Resource
    private HttpServletRequest request;

    /**
     * 查询标准工序列表
     */
//    @PreAuthorize("@ss.hasPermi('business:process:list')")
    @Log(title = "标准工序查询", businessType = BusinessType.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(BizProcess bizProcess) throws NoSuchFieldException, IllegalAccessException {
        startPage();
        List<BizProcess> list = bizProcessService.getList(bizProcess);

        return getDataTable(list);
    }
    /**
     * 查询标准工序列表
     */
    @Log(title = "当前工段没有配置的工序信息查询", businessType = BusinessType.SELECT)
    @GetMapping("/listFilterProcess")
    public TableDataInfo listFilterProcess(BizProcess bizProcess) throws NoSuchFieldException, IllegalAccessException {
        startPage();
        List<BizProcess> list = bizProcessService.getFilterList(bizProcess);
        return getDataTable(list);
    }

    /**
     * 获取标准工序详细信息
     */
//    @PreAuthorize("@ss.hasPermi('business:process:query')")
    @GetMapping(value = "/{id}")
    @Log(title = "标准工序查询详细信息", businessType = BusinessType.SELECT)
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(bizProcessService.getById(id));
    }

    /**
     * 新增标准工序
     */
//    @PreAuthorize("@ss.hasPermi('business:process:add')")
    @Log(title = "标准工序新增", businessType = BusinessType.INSERT)
    @PostMapping(value = "/save")
    public AjaxResult save(@RequestBody BizProcess bizProcess) throws NoSuchFieldException, IllegalAccessException {
        //修改标准工序
        if(bizProcess.getId()!=null) {
            QueryWrapper<BizProcess> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("process_code", bizProcess.getProcessCode());
            queryWrapper.eq("deleted", 0);
            queryWrapper.ne("id", bizProcess.getId());
            if (bizProcessService.list(queryWrapper).size() > 0) {
                return error("该工序编码已存在");
            }
            QueryWrapper<BizProcess> query = new QueryWrapper<>();
            query.eq("process_alias", bizProcess.getProcessAlias());
            query.eq("deleted", 0);
            query.ne("id", bizProcess.getId());
            if (bizProcessService.list(query).size() > 0) {
                return error("该工序简称已存在");
            }
        }
        //新增标准工序
        else {
            QueryWrapper<BizProcess> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("process_code", bizProcess.getProcessCode());
            queryWrapper.eq("deleted", 0);
            if (bizProcessService.list(queryWrapper).size() > 0) {
                return error("该工序编码已存在");
            }
            QueryWrapper<BizProcess> query = new QueryWrapper<>();
            query.eq("process_alias", bizProcess.getProcessAlias());
            query.eq("deleted", 0);
            if (bizProcessService.list(query).size() > 0) {
                return error("该工序简称已存在");
            }
        }
        return toAjax(bizProcessService.saveOrUpdate(bizProcess));
    }

    /**
     * 删除标准工序
     */
//    @PreAuthorize("@ss.hasPermi('business:process:remove')")
    @Log(title = "标准工序删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        QueryWrapper<BizTechnologicalProcessDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("process_id",ids.get(0));
        queryWrapper.eq("deleted",0);
        List<BizTechnologicalProcessDetail> list = bizTechnologicalProcessDetailService.list(queryWrapper);
        if (list.size() > 0) {
            return error("该工序已被应用与工艺中，不能删除");
        }
        return toAjax(bizProcessService.removeBatchByIds(ids));
    }
}
