package com.yeejoin.amos.boot.module.ugp.biz.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yeejoin.amos.boot.biz.common.utils.NameUtils;
import com.yeejoin.amos.boot.module.ugp.api.Enum.ProblemTypeEnum;
import com.yeejoin.amos.boot.module.ugp.api.entity.QualityProblem;
import com.yeejoin.amos.boot.module.ugp.biz.service.impl.ProblemInitiationServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RestController;
import com.yeejoin.amos.boot.biz.common.controller.BaseController;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.yeejoin.amos.boot.module.ugp.biz.service.impl.QualityProblemServiceImpl;
import org.typroject.tyboot.core.foundation.utils.ValidationUtil;
import org.typroject.tyboot.core.restful.utils.ResponseHelper;
import org.typroject.tyboot.core.restful.utils.ResponseModel;
import org.springframework.beans.factory.annotation.Autowired;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yeejoin.amos.boot.module.ugp.api.dto.QualityProblemDto;
import org.typroject.tyboot.core.restful.doc.TycloudOperation;
import org.typroject.tyboot.core.foundation.enumeration.UserType;

/**
 * 质量问题表
 *
 * @author system_generator
 * @date 2022-09-29
 */
@RestController
@Api(tags = "质量问题表Api")
@RequestMapping(value = "/quality-problem")
public class QualityProblemController extends BaseController {

	@Autowired
	ProblemInitiationServiceImpl problemInitiationService;

    @Autowired
    QualityProblemServiceImpl qualityProblemServiceImpl;

    /**
     * 新增质量问题表
     *
     * @return
     */
    @TycloudOperation(ApiLevel = UserType.AGENCY)
	@PostMapping(value = "/save")
	@ApiOperation(httpMethod = "POST", value = "新增质量问题表", notes = "新增质量问题表")
	public ResponseModel<QualityProblemDto> save(@RequestBody QualityProblemDto model) {
	    model = qualityProblemServiceImpl.createWithModel(model);
		QualityProblem qualityProblem = new QualityProblem();
		BeanUtils.copyProperties(model, qualityProblem);
		problemInitiationService.start(qualityProblem);
	    return ResponseHelper.buildResponse(model);
	}

	/**
     * 根据sequenceNbr更新
     *
     * @param sequenceNbr 主键
     * @return
     */
	@TycloudOperation(ApiLevel = UserType.AGENCY)
	@PutMapping(value = "/{sequenceNbr}")
	@ApiOperation(httpMethod = "PUT", value = "根据sequenceNbr更新质量问题表", notes = "根据sequenceNbr更新质量问题表")
	public ResponseModel<QualityProblemDto> updateBySequenceNbrQualityProblem(@RequestBody QualityProblemDto model,@PathVariable(value = "sequenceNbr") Long sequenceNbr) {
	    model.setSequenceNbr(sequenceNbr);
	    return ResponseHelper.buildResponse(qualityProblemServiceImpl.updateWithModel(model));
	}

	/**
     * 根据sequenceNbr删除
     *
     * @param sequenceNbr 主键
     * @return
     */
    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @DeleteMapping(value = "/{sequenceNbr}")
    @ApiOperation(httpMethod = "DELETE", value = "根据sequenceNbr删除质量问题表", notes = "根据sequenceNbr删除质量问题表")
    public ResponseModel<Boolean> deleteBySequenceNbr(HttpServletRequest request, @PathVariable(value = "sequenceNbr") Long sequenceNbr){
        return ResponseHelper.buildResponse(qualityProblemServiceImpl.removeById(sequenceNbr));
    }

    /**
     * 根据sequenceNbr查询
     *
	 * @param sequenceNbr  主键
     * @return
     */
	@TycloudOperation(ApiLevel = UserType.AGENCY)
	@GetMapping(value = "/{sequenceNbr}")
	@ApiOperation(httpMethod = "GET",value = "根据sequenceNbr查询单个质量问题表",  notes = "根据sequenceNbr查询单个质量问题表")
	public ResponseModel<QualityProblemDto> selectOne(@PathVariable Long sequenceNbr) {
	    return ResponseHelper.buildResponse(qualityProblemServiceImpl.queryBySeq(sequenceNbr));
	}

	/**
     * 列表分页查询
     *
     * @param current 当前页
     * @param current 每页大小
     * @return
     */
	@TycloudOperation(ApiLevel = UserType.AGENCY)
	@GetMapping(value = "/page")
	@ApiOperation(httpMethod = "GET",value = "质量问题表分页查询",  notes = "质量问题表分页查询")
	public ResponseModel<Page<QualityProblemDto>> queryForPage(@RequestParam(value = "current") int current,@RequestParam
                    (value = "size") int size) {
	    Page<QualityProblemDto> page = new Page<QualityProblemDto>();
	    page.setCurrent(current);
	    page.setSize(size);
	    return ResponseHelper.buildResponse(qualityProblemServiceImpl.queryForQualityProblemPage(page));
	}

	/**
	 * 列表全部数据查询
     *
	 * @return
	 */
	@TycloudOperation(ApiLevel = UserType.AGENCY,needAuth = false)
	@ApiOperation(httpMethod = "GET",value = "质量问题表列表全部数据查询",   notes = "质量问题表列表全部数据查询")
	@GetMapping(value = "/list")
	public ResponseModel<List<QualityProblemDto>> selectForList() {
	    return ResponseHelper.buildResponse(qualityProblemServiceImpl.queryForQualityProblemList());
	}

	/**
	 * 列表分页查询
	 * @return
	 */
	@TycloudOperation(ApiLevel = UserType.AGENCY,needAuth = false)
	@RequestMapping(value = "/pageList", method = RequestMethod.GET)
	@ApiOperation(httpMethod = "GET", value = "列表分页查询", notes = "列表分页查询")
	public ResponseModel<IPage<QualityProblemDto>> getItems(String current, String size,QualityProblemDto qualityProblemDto){
		return ResponseHelper.buildResponse(qualityProblemServiceImpl.getItems(current,size,qualityProblemDto));
	}

	@TycloudOperation(ApiLevel = UserType.AGENCY)
	@PostMapping(value = "/updateItem")
	@ApiOperation(httpMethod = "POST", value = "更新质量问题表")
	public ResponseModel<String> updateItem(@RequestBody QualityProblemDto qualityProblemDto,Long sequenceNbr){
		qualityProblemDto.setSolution(JSON.toJSONString(qualityProblemDto.getSolutionList()));
		qualityProblemDto.setReformDoc(JSON.toJSONString(qualityProblemDto.getReformDocList()));
		qualityProblemDto.setHandingOpinion(JSON.toJSONString(qualityProblemDto.getHandingOpinionList()));
		if(!ValidationUtil.isEmpty(qualityProblemDto.getReviewStatusString())){
			qualityProblemDto.setReviewStatus(false);
			if(ProblemTypeEnum.不存在问题.getCode().equals(qualityProblemDto.getReviewStatusString())){
				qualityProblemDto.setReviewStatus(true);
			}
		}

		QualityProblemDto qualityProblem = new QualityProblemDto();
		BeanUtils.copyProperties(qualityProblemDto,qualityProblem);
		return ResponseHelper.buildResponse(qualityProblemServiceImpl.updateItem(qualityProblem,sequenceNbr));
	}

	/**
	 * 质量问题详情显示
	 * @return
	 */
	@TycloudOperation(ApiLevel = UserType.AGENCY,needAuth = false)
	@RequestMapping(value = "/getInfoBySequenceNbr", method = RequestMethod.GET)
	@ApiOperation(httpMethod = "GET", value = "质量问题详情显示", notes = "质量问题详情显示")
	public ResponseModel<QualityProblemDto> getInfoBySequenceNbr(String sequenceNbr){
		return ResponseHelper.buildResponse(qualityProblemServiceImpl.getInfoBySequenceNbr(sequenceNbr));
	}

	@TycloudOperation(ApiLevel = UserType.AGENCY,needAuth = false)
	@RequestMapping(value = "/start", method = RequestMethod.POST)
	@ApiOperation(httpMethod = "POST", value = "启动流程", notes = "启动流程")
	public ResponseModel<String> start(@RequestBody QualityProblem qualityProblem){
		return ResponseHelper.buildResponse(problemInitiationService.start(qualityProblem));
	}

	@TycloudOperation(ApiLevel = UserType.AGENCY,needAuth = false)
	@RequestMapping(value = "/execute", method = RequestMethod.POST)
	@ApiOperation(httpMethod = "POST", value = "启动流程", notes = "启动流程")
	public ResponseModel<String> execute(String instanceId,@RequestBody QualityProblem objectd, String option){
		problemInitiationService.execute(instanceId,objectd,option);
		return ResponseHelper.buildResponse("ok");
	}



	/**
	 * 质量问题统计
	 */
	@TycloudOperation(ApiLevel = UserType.AGENCY,needAuth = false)
	@GetMapping(value = "/qualityProblem")
	@ApiOperation(httpMethod = "GET",value = "质量问题统计",  notes = "质量问题统计")
	public ResponseModel<List<Map<String,String>>> qualityProblemStatistics() {
		return ResponseHelper.buildResponse(qualityProblemServiceImpl.qualityProblemStatistics());
	}


	@TycloudOperation(ApiLevel = UserType.AGENCY, needAuth = false)
	@RequestMapping(value = "/manualProcessingStart", method = RequestMethod.POST)
	@ApiOperation(httpMethod = "POST", value = "启动人工处置流程", notes = "启动人工处置流程")
	public ResponseModel<Object> manualProcessingStart(@RequestBody QualityProblemDto qualityProblemDto,
													   @RequestParam("status") String status,
													   @RequestParam("type") String type) {

		QualityProblem qualityProblem = new QualityProblem();
		BeanUtils.copyProperties(qualityProblemDto, qualityProblem, "systemContactSheet", "submission");
		qualityProblem.setSystemContactSheet(JSON.toJSONString(qualityProblemDto.getSystemContactSheet()));
		qualityProblem.setSubmission(JSON.toJSONString(qualityProblemDto.getSubmission()));
		qualityProblem.setReformDoc(JSON.toJSONString(qualityProblemDto.getReformDocList()));
		return ResponseHelper.buildResponse(problemInitiationService.manualProcessingStart(qualityProblem, status, type));
	}

	@TycloudOperation(ApiLevel = UserType.AGENCY, needAuth = false)
	@RequestMapping(value = "/manualProcessingExecute", method = RequestMethod.POST)
	@ApiOperation(httpMethod = "POST", value = "执行人工处置流程", notes = "执行人工处置流程")
	public ResponseModel<String> manualProcessingExecute(@RequestBody QualityProblemDto qualityProblemDto,
														 @RequestParam("status") String status,
														 @RequestParam(required = false, value = "instanceId") String instanceId) {
		QualityProblem qualityProblem = new QualityProblem();
		BeanUtils.copyProperties(qualityProblemDto, qualityProblem, "systemContactSheet", "submission");
		qualityProblem.setSystemContactSheet(JSON.toJSONString(qualityProblemDto.getSystemContactSheet()));
		qualityProblem.setSubmission(JSON.toJSONString(qualityProblemDto.getSubmission()));
		qualityProblem.setReformDoc(JSON.toJSONString(qualityProblemDto.getReformDocList()));
		problemInitiationService.manualProcessingExecute(qualityProblem.getInstanceId(), qualityProblem, status);
		return ResponseHelper.buildResponse("ok");
	}

}
