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

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yeejoin.amos.boot.biz.common.controller.BaseController;
import com.yeejoin.amos.boot.module.jyjc.api.dto.PipelineInspectionResultDto;
import com.yeejoin.amos.boot.module.jyjc.api.enums.ResultTypeEnum;
import com.yeejoin.amos.boot.module.jyjc.api.model.JyjcInspectionResultDataModel;
import com.yeejoin.amos.boot.module.jyjc.api.model.JyjcInspectionResultModel;
import com.yeejoin.amos.boot.module.jyjc.biz.service.impl.JyjcInspectionResultServiceImpl;
import com.yeejoin.amos.feign.systemctl.model.DictionarieModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import org.typroject.tyboot.component.event.RestEventTrigger;
import org.typroject.tyboot.core.foundation.enumeration.UserType;
import org.typroject.tyboot.core.restful.doc.TycloudOperation;
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.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author system_generator
 * @date 2023-12-14
 */
@RestController
@Api(tags = "报检结果Api")
@RequestMapping(value = "/jyjc-inspection-result")
public class JyjcInspectionResultController extends BaseController {

    @Autowired
    JyjcInspectionResultServiceImpl jyjcInspectionResultServiceImpl;

    /**
     * 新增
     *
     * @return
     */
    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @PostMapping(value = "/save")
    @ApiOperation(httpMethod = "POST", value = "新增", notes = "新增")
    public ResponseModel<JyjcInspectionResultModel> save(@RequestBody JyjcInspectionResultModel model) {
        model = jyjcInspectionResultServiceImpl.createWithModel(model);
        return ResponseHelper.buildResponse(model);
    }

    /**
     * 根据sequenceNbr更新
     *
     * @param sequenceNbr 主键
     * @return
     */
    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @PutMapping(value = "/{sequenceNbr}")
    @ApiOperation(httpMethod = "PUT", value = "根据sequenceNbr更新", notes = "根据sequenceNbr更新")
    public ResponseModel<JyjcInspectionResultModel> updateBySequenceNbrJyjcInspectionResult(@RequestBody JyjcInspectionResultModel model, @PathVariable(value = "sequenceNbr") Long sequenceNbr) {
        model.setSequenceNbr(sequenceNbr);
        return ResponseHelper.buildResponse(jyjcInspectionResultServiceImpl.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(jyjcInspectionResultServiceImpl.removeById(sequenceNbr));
    }

    /**
     * 根据sequenceNbr查询
     *
     * @param sequenceNbr 主键
     * @return
     */
    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @GetMapping(value = "/{sequenceNbr}")
    @ApiOperation(httpMethod = "GET", value = "根据sequenceNbr查询单个", notes = "根据sequenceNbr查询单个")
    public ResponseModel<Map<String, Object>> selectOne(@PathVariable Long sequenceNbr) {
        return ResponseHelper.buildResponse(jyjcInspectionResultServiceImpl.queryDetailBySeq(sequenceNbr));
    }


    /**
     * 列表全部数据查询
     *getJyjcPersonByUnitCode
     * @return
     */
    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @ApiOperation(httpMethod = "GET", value = "列表全部数据查询", notes = "列表全部数据查询")
    @GetMapping(value = "/list")
    public ResponseModel<List<JyjcInspectionResultModel>> selectForList() {
        return ResponseHelper.buildResponse(jyjcInspectionResultServiceImpl.queryForJyjcInspectionResultList());
    }

    /**
     * 列表页面(报检单位）
     *
     * @param current 当前页
     * @param size    当前页大小
     * @param model   查询参数
     * @return
     */
    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @PostMapping(value = "/applicationUnitPage")
    @ApiOperation(httpMethod = "POST", value = "列表页面(报检单位）", notes = "列表页面(报检单位）")
    public ResponseModel<Page<JyjcInspectionResultModel>> applicationUnitQueryForPage(
            @RequestParam int current, @RequestParam int size,
            @RequestBody JyjcInspectionResultModel model) {
        Page<JyjcInspectionResultModel> page = new Page<>();
        page.setCurrent(current);
        page.setSize(size);
        return ResponseHelper.buildResponse(jyjcInspectionResultServiceImpl.queryForJyjcInspectionResultPage(page, model, false));
    }

    /**
     * 列表页面(检验检测单位）
     *
     * @param current 当前页
     * @param size    当前页大小
     * @param model   查询参数
     * @return
     */
    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @PostMapping(value = "/inspectionUnitPage")
    @ApiOperation(httpMethod = "POST", value = "列表页面(检验检测单位）", notes = "列表页面(检验检测单位）")
    public ResponseModel<Page<JyjcInspectionResultModel>> inspectionUnitQueryForPage(
            @RequestParam int current, @RequestParam int size,
            @RequestBody JyjcInspectionResultModel model) {
        Page<JyjcInspectionResultModel> page = new Page<>();
        page.setCurrent(current);
        page.setSize(size);
        return ResponseHelper.buildResponse(jyjcInspectionResultServiceImpl.queryForJyjcInspectionResultPage(page, model, true));
    }


    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @PostMapping(value = "/page")
    @ApiOperation(httpMethod = "POST", value = "列表页面(检验检测单位、使用单位共用）", notes = "列表页面(检验检测单位、使用单位共用）")
    public ResponseModel<Page<JyjcInspectionResultModel>> pageList(@RequestParam int current, @RequestParam int size,
                                                                   @RequestParam(value = "sort", required = false) String sort,
                                                                   @RequestBody JyjcInspectionResultModel model) {
        Page<JyjcInspectionResultModel> page = new Page<>();
        page.setCurrent(current);
        page.setSize(size);
        return ResponseHelper.buildResponse(jyjcInspectionResultServiceImpl.queryForPageList(page, model, sort));
    }

    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @GetMapping(value = "/sourceResult")
    @ApiOperation(httpMethod = "GET", value = "列表筛选项-结果来源", notes = "列表筛选项-结果来源")
    public ResponseModel<List<JSONObject>> sourceResult() {
        return ResponseHelper.buildResponse(ResultTypeEnum.All());
    }



    /**
	 * 检验结果-上传结果和编辑结果
	 *
	 * @return
	 */
	@TycloudOperation(ApiLevel = UserType.AGENCY)
	@PutMapping(value = "/updateResult")
	@ApiOperation(httpMethod = "PUT", value = "检验结果-上传结果和编辑结果", notes = "检验结果-上传结果和编辑结果")
    public ResponseModel<JyjcInspectionResultModel> updateJyjcInspectionResult(@RequestBody Map<String, Map<String, Object>> model) {
		return ResponseHelper.buildResponse(jyjcInspectionResultServiceImpl.updateJyjcInspectionResult(model));
	}


    /**
     * 获取设备种类列表
     *
     * @return
     */
    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @ApiOperation(httpMethod = "GET", value = "获取设备种类列表", notes = "获取设备种类列表")
    @GetMapping(value = "/equipTypeList")
    public ResponseModel<List<Map<String, Object>>> equipTypeList(@RequestParam(required = false) String type, @RequestParam(required = false) String notNode) {
        return ResponseHelper.buildResponse(jyjcInspectionResultServiceImpl.equipList(type, notNode));
    }

    /**
     * 报检结果接收
     * @param resultDataModels 结果信息
     * @return List<JyjcInspectionResultDataModel>
     */
    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @ApiOperation(httpMethod = "POST", value = "非压力管道-报检结果接收", notes = "非压力管道-对外对接使用，支持单条及批量")
    @PostMapping(value = "/receive")
    @RestEventTrigger(value = "operateLogRestEventHandler")
    public ResponseModel<List<JyjcInspectionResultDataModel>> saveResultDataBatch(@RequestBody List<JyjcInspectionResultDataModel> resultDataModels){
        return ResponseHelper.buildResponse(jyjcInspectionResultServiceImpl.receivePushResultData(resultDataModels));
    }

    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @ApiOperation(httpMethod = "GET", value = "检验类型-按照身份查询", notes = "检验类型-按照身份查询")
    @GetMapping(value = "/inspectType/list")
    public ResponseModel<List<DictionarieModel>> inspectTypeListByPerson(){
        return ResponseHelper.buildResponse(jyjcInspectionResultServiceImpl.inspectTypeListByPerson(getSelectedOrgInfo()));
    }

    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @GetMapping(value = "/export")
    @ApiOperation(httpMethod = "GET", value = "检验检测结果列表数据导出", notes = "检验检测结果列表数据导出")
    public void Export(HttpServletResponse response, String ids) {
        Assert.hasText(ids,"未选择导出数据");
        jyjcInspectionResultServiceImpl.Export(response, Arrays.asList(ids.split(",")));
    }

    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @PostMapping(value = "/useUnitEntryWithSave")
    @ApiOperation(httpMethod = "POST", value = "使用单位录入检验结果-保存", notes = "使用单位录入检验结果-保存")
    public ResponseModel<Boolean> useUnitEntryWithSave(@RequestBody Map<String, Object> entryData) {
        return ResponseHelper.buildResponse(jyjcInspectionResultServiceImpl.useUnitEntryWithSave(entryData,getSelectedOrgInfo()));
    }

    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @PostMapping(value = "/useUnitEntryWithUpdate")
    @ApiOperation(httpMethod = "POST", value = "使用单位录入检验结果-更新", notes = "使用单位录入检验结果-更新")
    public ResponseModel<Boolean> useUnitEntryWithUpdate(@RequestBody Map<String, Object> entryData) {
        return ResponseHelper.buildResponse(jyjcInspectionResultServiceImpl.useUnitEntryWithUpdate(entryData,getSelectedOrgInfo()));
    }

    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @GetMapping(value = "/getUseUnitEntryDetail/{sequenceNbr}")
    @ApiOperation(httpMethod = "GET", value = "使用单位录入检验结果-详情", notes = "使用单位录入检验结果-详情")
    public ResponseModel<JSONObject> getUseUnitEntryDetail(@PathVariable(value = "sequenceNbr") Long resultSeq) {
        return ResponseHelper.buildResponse(jyjcInspectionResultServiceImpl.getUseUnitEntryDetail(resultSeq));
    }

    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @DeleteMapping(value = "/delUseUnitEntry")
    @ApiOperation(httpMethod = "DELETE", value = "使用单位录入检验结果-单条删除", notes = "使用单位录入检验结果-单条删除")
    public ResponseModel<Boolean> delUseUnitEntry(@RequestParam(value = "sequenceNbr")String resultSeq) {
        return ResponseHelper.buildResponse(jyjcInspectionResultServiceImpl.delUseUnitEntry(Long.valueOf(resultSeq)));
    }

    /**
     * 压力管道-报检结果接收
     * @param resultData 结果信息
     * @return List<JyjcInspectionResultDataModel>
     */
    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @ApiOperation(httpMethod = "POST", value = "压力管道-报检结果接收", notes = "压力管道-对外对接使用，支持单条及批量")
    @PostMapping(value = "/pipeline/receive")
    @RestEventTrigger(value = "operateLogRestEventHandler")
    public ResponseModel<List<PipelineInspectionResultDto>> savePipelineResultDataBatch(@RequestBody List<PipelineInspectionResultDto> resultData){
        return ResponseHelper.buildResponse(jyjcInspectionResultServiceImpl.receivePipelineResultData(resultData));
    }
}
