package org.jeecg.modules.workOrder.controller;

import java.util.Arrays;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.workOrder.entity.WorkPermitComplete;
import org.jeecg.modules.workOrder.service.IWorkPermitCompleteService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 请销点记录
 * @Author: jeecg-boot
 * @Date:   2025-02-14
 * @Version: V1.0
 */
@Api(tags="请销点记录")
@RestController
@RequestMapping("/workOrder/workPermitComplete")
@Slf4j
public class WorkPermitCompleteController extends JeecgController<WorkPermitComplete, IWorkPermitCompleteService> {
	@Autowired
	private IWorkPermitCompleteService workPermitCompleteService;

	/**
	 * 分页列表查询
	 *
	 * @param workPermitComplete
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "请销点记录-分页列表查询")
	@ApiOperation(value="请销点记录-分页列表查询", notes="请销点记录-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<WorkPermitComplete>> queryPageList(WorkPermitComplete workPermitComplete,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<WorkPermitComplete> queryWrapper = QueryGenerator.initQueryWrapper(workPermitComplete, req.getParameterMap());
		Page<WorkPermitComplete> page = new Page<WorkPermitComplete>(pageNo, pageSize);
		IPage<WorkPermitComplete> pageList = workPermitCompleteService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param workPermitComplete
	 * @return
	 */
	@AutoLog(value = "请销点记录-添加")
	@ApiOperation(value="请销点记录-添加", notes="请销点记录-添加")
	//@RequiresPermissions("org.jeecg.modules:bu_work_permit_complete:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody WorkPermitComplete workPermitComplete) {
		int result = workPermitCompleteService.addPermit(workPermitComplete);
		if (result == 1) {
			return Result.OK("请点成功！");
		} else if (result == 0) {
			return Result.error("物资申请未通过！");
		}
		return Result.error("请点失败！");
	}

	/**
	 *  编辑
	 *
	 * @param workPermitComplete
	 * @return
	 */
	@AutoLog(value = "请销点记录-编辑")
	@ApiOperation(value="请销点记录-编辑/销点", notes="请销点记录-编辑/销点")
	//@RequiresPermissions("org.jeecg.modules:bu_work_permit_complete:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody WorkPermitComplete workPermitComplete) {
		int result = workPermitCompleteService.updateComple(workPermitComplete);
		if (result == 1) {
			return Result.OK("销点成功！");
		} else if (result == 0) {
			return Result.error("请点站值班员不能为空！");
		} else if (result == 2) {
			return Result.error("编辑成功！");
		}
		return Result.error("失败！");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "请销点记录-通过id删除")
	@ApiOperation(value="请销点记录-通过id删除", notes="请销点记录-通过id删除")
	//@RequiresPermissions("org.jeecg.modules:bu_work_permit_complete:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		workPermitCompleteService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "请销点记录-批量删除")
	@ApiOperation(value="请销点记录-批量删除", notes="请销点记录-批量删除")
	//@RequiresPermissions("org.jeecg.modules:bu_work_permit_complete:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.workPermitCompleteService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "请销点记录-通过id查询")
	@ApiOperation(value="请销点记录-通过id查询", notes="请销点记录-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<WorkPermitComplete> queryById(@RequestParam(name="id",required=true) String id) {
		WorkPermitComplete workPermitComplete = workPermitCompleteService.getById(id);
		if(workPermitComplete==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(workPermitComplete);
	}

	 /**
	  * 通过workOrder查询
	  *
	  * @param workOrder
	  * @return
	  */
	 @ApiOperation(value="请销点记录-通过workOrder查询", notes="请销点记录-通过workOrder查询")
	 @GetMapping(value = "/queryByWorkOrder")
	 public Result<WorkPermitComplete> queryByWorkOrder(@RequestParam(name="workOrder",required=true) String workOrder) {
		 WorkPermitComplete workPermitComplete = workPermitCompleteService.getOne(new LambdaQueryWrapper<WorkPermitComplete>().eq(WorkPermitComplete::getWorkOrder,workOrder));
		 if(workPermitComplete==null) {
			 return Result.error("未找到请销点记录");
		 }
		 return Result.OK(workPermitComplete);
	 }

    /**
    * 导出excel
    *
    * @param request
    * @param workPermitComplete
    */
    //@RequiresPermissions("org.jeecg.modules:bu_work_permit_complete:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, WorkPermitComplete workPermitComplete) {
        return super.exportXls(request, workPermitComplete, WorkPermitComplete.class, "请销点记录");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("bu_work_permit_complete:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, WorkPermitComplete.class);
    }

}
