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

import cn.hutool.core.bean.BeanUtil;
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.common.biz.utils.CommonResponseUtil;
import com.yeejoin.amos.boot.module.jg.api.dto.JgMaintainNoticeDto;
import com.yeejoin.amos.boot.module.jg.api.entity.JgMaintainNotice;
import com.yeejoin.amos.boot.module.jg.api.service.IJgMaintainNoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import jdk.nashorn.api.scripting.JSObject;
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.restful.doc.TycloudOperation;
import org.typroject.tyboot.core.restful.utils.ResponseHelper;
import org.typroject.tyboot.core.restful.utils.ResponseModel;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 维修告知
 *
 * @author system_generator
 * @date 2023-12-12
 */
@RestController
@Api(tags = "维修告知Api")
@RequestMapping(value = "/jg-maintain-notice")
public class JgMaintainNoticeController extends BaseController {

    @Autowired
    private IJgMaintainNoticeService iJgMaintainNoticeService;

    private static final String TABLE_PAGE_ID = "maintainInfo";
    /**
     * 新增
     *
     * @return
     */
    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @PostMapping(value = "/save")
    @ApiOperation(httpMethod = "POST", value = "新增维修告知", notes = "新增维修告知")
    public ResponseModel<Object> save(@RequestParam String submitType, @RequestBody Map<String, Object> model) {
        ReginParams reginParams = getSelectedOrgInfo();
        return ResponseHelper.buildResponse(iJgMaintainNoticeService.saveNotice(submitType, model, reginParams));
    }

    /**
     * 根据sequenceNbr更新
     *
     * @param model 维修告知
     */
    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @PutMapping(value = "/update")
    @ApiOperation(httpMethod = "PUT", value = "根据sequenceNbr更新维修告知", notes = "根据sequenceNbr更新维修告知")
    public ResponseModel<JgMaintainNoticeDto> updateBySequenceNbrJgMaintainNotice(@RequestParam String submitType, @RequestBody Map<String, Object> model, @RequestParam(value = "op", required = false) String op) {
        JgMaintainNoticeDto maintainInfo = BeanUtil.mapToBean(((LinkedHashMap) model.get(TABLE_PAGE_ID)), JgMaintainNoticeDto.class, true);
        if (Objects.isNull(maintainInfo)) {
            throw new IllegalArgumentException("参数maintainInfo不能为空");
        }
        Object o = ((LinkedHashMap<?, ?>) model.get(TABLE_PAGE_ID)).get("constructionContractList");
        Object o1 = ((LinkedHashMap<?, ?>) model.get(TABLE_PAGE_ID)).get("powerOfAttorneyList");
        maintainInfo.setConstructionContractList((List<Map<String, Object>>) o);
        maintainInfo.setPowerOfAttorneyList((List<Map<String, Object>>) o1);
        return ResponseHelper.buildResponse(iJgMaintainNoticeService.updateMaintainNotice(submitType, maintainInfo, op));
    }

    /**
     * 根据sequenceNbr删除
     *
     * @param sequenceNbr 主键
     */
    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @DeleteMapping(value = "/delete")
    @ApiOperation(httpMethod = "DELETE", value = "根据sequenceNbr删除维修告知", notes = "根据sequenceNbr删除维修告知")
    public ResponseModel<Boolean> deleteBySequenceNbr(@RequestParam(value = "sequenceNbr") Long[] sequenceNbr) {
        return ResponseHelper.buildResponse(iJgMaintainNoticeService.deleteBySequenceNbr(sequenceNbr));
    }

    /**
     * 根据sequenceNbr批量删除
     *
     * @param sequenceNbrs 主键
     */
    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @DeleteMapping(value = "/batchDelete")
    @ApiOperation(value = "根据sequenceNbr删除维保合同备案", notes = "根据sequenceNbr删除维保合同备案")
    public ResponseModel<Boolean> deleteForBatch(@RequestParam("sequenceNbrs") Long[] sequenceNbrs) {
        try {
            return ResponseHelper.buildResponse(iJgMaintainNoticeService.deleteForBatch(sequenceNbrs));
        } catch (Exception e) {
            return CommonResponseUtil.failure(e.getMessage());
        }
    }

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

    /**
     * 列表分页查询
     *
     * @param current 当前页
     * @param current 每页大小
     * @return
     */
    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @PostMapping(value = "/page")
    @ApiOperation(httpMethod = "POST", value = "维修告知分页查询", notes = "维修告知分页查询")
    public ResponseModel<Page<JgMaintainNoticeDto>> queryForPage(
            @ApiParam(value = "当前页码", required = true) @RequestParam(value = "current", defaultValue = "1") int current,
            @ApiParam(value = "每页大小", required = true) @RequestParam(value = "size", defaultValue = "20") int size,
            @RequestBody(required = false) JgMaintainNoticeDto model
    ) {
        Page<JgMaintainNotice> page = new Page<>(current, size);
        ReginParams reginParams = getSelectedOrgInfo();
        return ResponseHelper.buildResponse(iJgMaintainNoticeService.queryForJgMaintainNoticePage(page, model, reginParams));
    }

    /**
     * 生成告知单
     */
    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @ApiOperation(httpMethod = "GET", value = "维修告知列表全部数据查询", notes = "维修告知列表全部数据查询")
    @GetMapping(value = "/generate-report")
    public ResponseModel<String> generateReport(@RequestParam("sequenceNbr") Long sequenceNbr) {
        return ResponseHelper.buildResponse(iJgMaintainNoticeService.generateMaintainNoticeReport(sequenceNbr));
    }
}
