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

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yeejoin.amos.boot.biz.common.bo.ReginParams;
import com.yeejoin.amos.boot.biz.common.controller.BaseController;
import com.yeejoin.amos.boot.module.jg.api.dto.JgChangeRegistrationTransferDto;
import com.yeejoin.amos.boot.module.jg.api.service.IJgChangeRegistrationTransferService;
import com.yeejoin.amos.boot.module.jg.api.service.IJgInstallationNoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.typroject.tyboot.core.foundation.enumeration.UserType;
import org.typroject.tyboot.core.foundation.utils.ValidationUtil;
import org.typroject.tyboot.core.restful.doc.TycloudOperation;
import org.typroject.tyboot.core.restful.exception.instance.BadRequest;
import org.typroject.tyboot.core.restful.utils.ResponseHelper;
import org.typroject.tyboot.core.restful.utils.ResponseModel;

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

/**
 * 移装变更登记登记
 *
 * @author system_generator
 * @date 2023-12-20
 */
@RestController
@Api(tags = "移装变更登记登记Api")
@RequestMapping(value = "/jg-change-registration-transfer")
public class JgChangeRegistrationTransferController extends BaseController {

	
    @Autowired
	IJgChangeRegistrationTransferService jgChangeRegistrationTransferService;

    /**
     * 新增移装变更登记登记
     *
     * @return
     */
    @TycloudOperation(ApiLevel = UserType.AGENCY)
	@PostMapping(value = "/save")
	@ApiOperation(httpMethod = "POST", value = "新增移装变更登记登记", notes = "新增移装变更登记登记")
	public ResponseModel<Object> save(@RequestParam(value = "submitType") String submitType,
									   @RequestBody JSONObject map) {
		ReginParams reginParams = getSelectedOrgInfo();
	    return ResponseHelper.buildResponse(jgChangeRegistrationTransferService.createTransfer(submitType, map, reginParams));
	}

	/**
     * 根据sequenceNbr更新
     *
     * @param submitType
     * @return
     */
	@TycloudOperation(ApiLevel = UserType.AGENCY)
	@PutMapping(value = "/update")
	@ApiOperation(httpMethod = "PUT", value = "根据sequenceNbr更新移装变更登记登记", notes = "根据sequenceNbr更新移装变更登记登记")
	public ResponseModel<Object> updateBySequenceNbr(@RequestParam(value = "submitType") String submitType,
													 @RequestBody JSONObject map) {
		ReginParams reginParams = getSelectedOrgInfo();
	    return ResponseHelper.buildResponse(jgChangeRegistrationTransferService.updateTransfer(submitType, map, reginParams));
	}

	@TycloudOperation(ApiLevel = UserType.AGENCY)
	@PostMapping(value = "/flowExecute")
	@ApiOperation(httpMethod = "POST", value = "执行流程", notes = "执行流程")
	public ResponseModel<Object> flowExecute(@RequestBody JSONObject map) {
		jgChangeRegistrationTransferService.flowExecute(Long.valueOf(String.valueOf(map.get("sequenceNbr"))),
				String.valueOf(map.get("instanceId")),
				String.valueOf(map.get("operate")),
				String.valueOf(map.get("opinion")),
				String.valueOf(map.get("nextTaskId")));
		return ResponseHelper.buildResponse("ok");
	}

	@TycloudOperation(ApiLevel = UserType.AGENCY)
	@PostMapping(value = "/revocation")
	@ApiOperation(httpMethod = "POST", value = "撤回", notes = "撤回")
	public ResponseModel<Object> revocation(@RequestBody JSONObject map) {
		jgChangeRegistrationTransferService.revocation(String.valueOf(map.get("instanceId")),
				String.valueOf(map.get("nextTaskId")));
		return ResponseHelper.buildResponse("ok");
	}

	/**
	 * 根据sequenceNbrs批量删除
	 *
	 * @param sequenceNbrs 主键
	 * @return
	 */
	@TycloudOperation(ApiLevel = UserType.AGENCY)
	@DeleteMapping(value = "/deteteBatch")
	@ApiOperation(httpMethod = "DELETE", value = "根据sequenceNbr删除移装变更登记登记", notes = "根据sequenceNbr删除移装变更登记登记")
	public ResponseModel<Boolean> deleteBySequenceNbr(HttpServletRequest request, @RequestParam(value = "sequenceNbrs") Long[] sequenceNbrs){
		return ResponseHelper.buildResponse(jgChangeRegistrationTransferService.deleteBatchBySequenceNbrs(sequenceNbrs));
	}

    /**
     * 根据sequenceNbr查询
     *
	 * @param sequenceNbr  主键
     * @return
     */
	@TycloudOperation(ApiLevel = UserType.AGENCY)
	@GetMapping(value = "/detail")
	@ApiOperation(httpMethod = "GET",value = "根据sequenceNbr查询单个移装变更登记登记",  notes = "根据sequenceNbr查询单个移装变更登记登记")
	public ResponseModel<Map<String, Map<String, Object>>> selectOne(@RequestParam(value = "sequenceNbr") String sequenceNbr) {
	    return ResponseHelper.buildResponse(jgChangeRegistrationTransferService.getTransferDetail(sequenceNbr, getSelectedOrgInfo().getCompany()));
	}

	/**
     * 列表分页查询
     *
     * @param current 当前页
     * @param current 每页大小
     * @return
     */
	@TycloudOperation(ApiLevel = UserType.AGENCY)
	@GetMapping(value = "/page")
	@ApiOperation(httpMethod = "GET",value = "移装变更登记登记分页查询",  notes = "移装变更登记登记分页查询")
	public ResponseModel<Page<Map<String, Object>>> queryForPage(@RequestParam(value = "current") int current,
																@RequestParam(value = "size") int size,
																 @RequestParam(value = "sort",required = false) String sort,
																JgChangeRegistrationTransferDto params) {
		Page<Map<String, Object>> page = new Page<>(current, size);
		ReginParams reginParams = getSelectedOrgInfo();
		String companyTypeCode = "";
		if(reginParams.getCompany().getLevel().equals(COMPANY_TYPE_COMPANY)){
			companyTypeCode = COMPANY_TYPE_COMPANY;
		} else {
			companyTypeCode = COMPANY_TYPE_SUPERVISION;
		}
		params.setUnitCode(reginParams.getCompany().getCompanyCode());
		return ResponseHelper.buildResponse(jgChangeRegistrationTransferService.queryListForPage(page,sort,params, companyTypeCode, reginParams.getCompany().getCompanyType()));
	}

	/**
	 * 导出使用登记证
	 * @param response 返回
	 * @param sequenceNbr 主键
	 * @param printType 打印类型，0-正常打印，1-套打（默认0）
	 */
	@TycloudOperation(ApiLevel = UserType.AGENCY)
	@GetMapping(value = "/export")
	@ApiOperation(httpMethod = "GET", value = "导出使用登记证", notes = "导出使用登记证")
	public void exportImageZip(HttpServletResponse response, @RequestParam("sequenceNbr") String sequenceNbr,
							   @RequestParam(value = "printType", defaultValue = "0") String printType){
		jgChangeRegistrationTransferService.exportUseRegistrationCertificate(sequenceNbr, response, printType);
	}
	@TycloudOperation(ApiLevel = UserType.AGENCY)
	@GetMapping (value = "/detailFieldCamelCase")
	@ApiOperation(httpMethod = "GET", value = "根据record查询设备注册信息详情，返回字段驼峰命名", notes = "根据record查询设备注册信息详情，返回字段驼峰命名")
	public ResponseModel<Map<String, Object>> getDetailFieldCamelCase(@RequestParam String record) {
		return ResponseHelper.buildResponse(jgChangeRegistrationTransferService.getDetailFieldCamelCase(record));
	}
}
