package com.tofly.qualitycontrol.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.qualitycontrol.dto.PlanDTO;
import com.tofly.qualitycontrol.dto.PlanReportDTO;
import com.tofly.qualitycontrol.entity.Plan;
import com.tofly.qualitycontrol.service.IPlanProcessService;
import com.tofly.qualitycontrol.service.IPlanService;
import com.tofly.qualitycontrol.vo.PlanAuditVO;
import com.tofly.qualitycontrol.vo.PlanManageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

import static com.tofly.qualitycontrol.common.QualityControlConstant.FAILED;
import static com.tofly.qualitycontrol.common.QualityControlConstant.SUCCESS;

/**
 * 质检计划接口控制器
 *
 * @author B
 */
@RestController
@RequestMapping("/plan")
@Api(value = "质检计划接口", tags = "质检计划接口")
@RequiredArgsConstructor
public class PlanController {

	private final IPlanService planService;
	private final IPlanProcessService planProcessService;

	@ApiOperation(value = "计划管理-添加质检计划,可上传附件", notes = "plan里传计划类型,计划时间,计划说明,上报人部门名称和id;文件上传可选", httpMethod = "POST")
	@PostMapping("/savePlan")
	public ResultRespone savePlan(@RequestPart Plan plan,@RequestPart(value = "fileList", required = false) List<MultipartFile> fileList) {
		if (CollectionUtils.isEmpty(fileList)) {
			return ResultRespone.failed(false,"请上传计划附件！附件必须上传");
		}
		Boolean flag = planService.savePlan(plan,fileList);
		return flag ? ResultRespone.success(flag,SUCCESS) : ResultRespone.failed(flag,FAILED);
	}

	@ApiOperation(value = "修改计划管理,可修改文件", notes = "", httpMethod = "POST")
	@PostMapping("/updatePlan")
	public ResultRespone updatePlan(@RequestPart Plan plan,@RequestPart(value = "fileList", required = false) List<MultipartFile> fileList) {
		Boolean flag = planService.updatePlan(plan,fileList);
		return flag ? ResultRespone.success(flag,SUCCESS) : ResultRespone.failed(flag,FAILED);
	}

	@ApiImplicitParams({
							   @ApiImplicitParam(value = "计划时间", name = "planTime", required = false, paramType = "query", dataType = "string", dataTypeClass = String.class),
							   @ApiImplicitParam(value = "计划状态", name = "planStatus", required = false, paramType = "query", dataType = "string", dataTypeClass = String.class)
					   })
	@ApiOperation(value = "计划管理-分页,查询", notes = "", httpMethod = "POST")
	@PostMapping("/getManageList")
	public ResultRespone<IPage<PlanManageVO>> getManageList(@RequestParam(required = false) String planTime,@RequestParam(required = false) String planStatus,Page<Plan> page) {
		return ResultRespone.success(planService.getManageList(planTime,planStatus,page),SUCCESS);
	}

	@ApiOperation(value = "计划审核-上报", notes = "包装类,id为质检计划主键id;planAuditorList为批量添加的审核人,每个对象传:每一级的等级(1,2,3,4...),审核人id,审核人姓名,审核人部门id,审核人部门名称", httpMethod = "POST")
	@PostMapping("/reportPlan")
	public ResultRespone reportPlan(@RequestBody PlanReportDTO planReportDTO) {
		Boolean flag = planService.reportPlan(planReportDTO);
		return flag ? ResultRespone.success(flag,SUCCESS) : ResultRespone.failed(flag,FAILED);
	}

	@ApiImplicitParams({
							   @ApiImplicitParam(value = "上报人部门id", name = "reporterDeptIdId", required = false, paramType = "query", dataType = "long", dataTypeClass = Long.class),
							   @ApiImplicitParam(value = "计划时间", name = "planTime", required = false, paramType = "query", dataType = "string", dataTypeClass = String.class),
							   @ApiImplicitParam(value = "计划状态", name = "planStatus", required = false, paramType = "query", dataType = "string", dataTypeClass = String.class)
					   })
	@ApiOperation(value = "计划审核-分页,查询", notes = "", httpMethod = "POST")
	@PostMapping("/getAuditList")
	public ResultRespone<IPage<PlanAuditVO>> getAuditList(@RequestParam(required = false) Long reporterDeptIdId,@RequestParam(required = false) String planTime,@RequestParam(required = false) String planStatus,Page<Plan> page) {
		return ResultRespone.success(planService.getAuditList(reporterDeptIdId,planTime,planStatus,page),SUCCESS);
	}

	@ApiImplicitParams({
							   @ApiImplicitParam(value = "质检计划主键id", name = "id", required = true, paramType = "query", dataType = "long", dataTypeClass = Long.class),
			                   @ApiImplicitParam(value = "查询类型  0:上报,1:信息", name = "type", required = true, paramType = "query", dataType = "long", dataTypeClass = Long.class)
					   })
	@ApiOperation(value = "计划审核-审核详情,包含计划信息和审核历史", notes = "", httpMethod = "GET")
	@GetMapping("/getDetailAndHistoryList")
	public ResultRespone<PlanDTO> getDetailAndHistoryList(@RequestParam Long id,@RequestParam Long type) {
		return ResultRespone.success(planService.getDetailAndHistoryList(id,type),SUCCESS);
	}

	@ApiImplicitParams({
							   @ApiImplicitParam(value = "质检计划主键id", name = "id", required = true, paramType = "query", dataType = "long", dataTypeClass = Long.class),
							   @ApiImplicitParam(value = "审核结论(字典)", name = "auditResult", required = true, paramType = "query", dataType = "string", dataTypeClass = String.class),
							   @ApiImplicitParam(value = "审核说明", name = "auditDesc", required = false, paramType = "query", dataType = "string", dataTypeClass = String.class)
					   })
	@ApiOperation(value = "计划审核-审核", notes = "", httpMethod = "GET")
	@GetMapping("/auditPlan")
	public ResultRespone auditPlan(@RequestParam Long id,@RequestParam String auditResult,@RequestParam(required = false) String auditDesc) {
		Boolean flag = planService.auditPlan(id,auditResult,auditDesc);
		return flag ? ResultRespone.success(flag,SUCCESS) : ResultRespone.failed(flag,FAILED);
	}

}
