package com.yunfeiyun.agmp.fms.controller;

import com.yunfeiyun.agmp.common.annotation.Log;
import com.yunfeiyun.agmp.common.constant.ErrorCode;
import com.yunfeiyun.agmp.common.core.controller.BaseController;
import com.yunfeiyun.agmp.common.core.domain.AjaxResult;
import com.yunfeiyun.agmp.common.core.page.TableDataInfo;
import com.yunfeiyun.agmp.common.enums.BusinessType;
import com.yunfeiyun.agmp.fms.domain.FmsMach;
import com.yunfeiyun.agmp.fms.domain.reqvo.FmsMachListReqVo;
import com.yunfeiyun.agmp.fms.domain.reqvo.FmsMachRecordListReqVo;
import com.yunfeiyun.agmp.fms.domain.resvo.FmsMachAppMapResVo;
import com.yunfeiyun.agmp.fms.domain.resvo.FmsMachRecordListResVo;
import com.yunfeiyun.agmp.fms.util.DataAuthUtil;
import com.yunfeiyun.agmp.fms.service.IFmsMachService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 机具Controller
 * 
 * @author 杨晓辉
 * @date 2023-05-25
 */
@RestController
@RequestMapping("/fms/mach")
public class FmsMachController extends BaseController
{
    @Autowired
    private IFmsMachService fmsMachService;
    @Resource
    private DataAuthUtil dataAuthUtil;

    /**
     * 查询机具列表
     */
    @PreAuthorize("@ss.hasPermi('fms:mach:list')")
    @GetMapping("/list")
    public TableDataInfo list(FmsMachListReqVo fmsMachListReqVo)
    {
        startPage();
        List<FmsMach> list = fmsMachService.selectFmsMachList(fmsMachListReqVo);
        return getDataTable(list);
    }

    /**
     * app端查询所有农资，无分页
     * */
    @PreAuthorize("@ss.hasPermi('fms:mach:app:select:json')")
    @GetMapping("/app/select/json")
    public AjaxResult appList()
    {
        List<FmsMachAppMapResVo> list = fmsMachService.selectFmsMachAppMapResVoList();
        return success(list);
    }

    /**
     * 查询机具使用记录列表
     */
    @PreAuthorize("@ss.hasPermi('fms:mach:record:list')")
    @GetMapping("/record/list")
    public TableDataInfo recordList(FmsMachRecordListReqVo fmsMachRecordListReqVo)
    {
        fmsMachRecordListReqVo.setBlockIds(dataAuthUtil.blockList());
        if(!dataAuthUtil.checkRole("warehouse_admin"))
        {
            fmsMachRecordListReqVo.setDataFilter(dataAuthUtil.isValidate());
        }
        startPage();
        if(fmsMachRecordListReqVo.getDataFilter()&&fmsMachRecordListReqVo.getBlockIds().isEmpty()){
            return getDataTable(new ArrayList<>());
        }
        List<FmsMachRecordListResVo> list = fmsMachService.selectFmsMachRecordList(fmsMachRecordListReqVo);
        return getDataTable(list);
    }

    /**
     * 新增机具
     */
    @PreAuthorize("@ss.hasPermi('fms:mach:add')")
    @Log(title = "机具", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody FmsMach fmsMach)
    {
        FmsMachListReqVo reqVo = new FmsMachListReqVo();
        reqVo.setMachName(fmsMach.getMachName());
        reqVo.setMachType(fmsMach.getMachType());
        List<FmsMach> list = fmsMachService.selectFmsMachList(reqVo);
        if (list.size() > 0) {
            return AjaxResult.error(ErrorCode.FMS_MACH_EXIST.getCode(), ErrorCode.FMS_MACH_EXIST.getMessage());
        }
        return toAjax(fmsMachService.insertFmsMach(fmsMach));
    }

    /**
     * 修改机具
     */
    @PreAuthorize("@ss.hasPermi('fms:mach:edit')")
    @Log(title = "机具", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody FmsMach fmsMach)
    {
        FmsMach fmsMach1 = fmsMachService.selectFmsMachByMachId(fmsMach.getMachId());
        if (!fmsMach1.getMachName().equals(fmsMach.getMachName()) || !fmsMach1.getMachType().equals(fmsMach.getMachType())) {
            FmsMachListReqVo reqVo = new FmsMachListReqVo();
            reqVo.setMachName(fmsMach.getMachName());
            reqVo.setMachType(fmsMach.getMachType());
            List<FmsMach> list = fmsMachService.selectFmsMachList(reqVo);
            if (list.size() > 0) {
                return AjaxResult.error(ErrorCode.FMS_MACH_EXIST.getCode(), ErrorCode.FMS_MACH_EXIST.getMessage());
            }
        }
        return toAjax(fmsMachService.updateFmsMach(fmsMach));
    }

    /**
     * 删除机具
     */
    @PreAuthorize("@ss.hasPermi('fms:mach:delete')")
    @Log(title = "机具", businessType = BusinessType.DELETE)
	@DeleteMapping("/delete")
    public AjaxResult remove(@RequestParam("machId") String machId)
    {
        return toAjax(fmsMachService.deleteFmsMachByMachId(machId));
    }
}
