package com.ruoyi.app.controller.workTicket;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.worktecket.dto.CheckWorkTicketMeasureDTO;
import com.ruoyi.worktecket.dto.WorkTicketBasicDataDTO;
import com.ruoyi.worktecket.dto.WorkTicketGasAnalysisDTO;
import com.ruoyi.worktecket.dto.res.WorkTicketBasicDataResultDTO;
import com.ruoyi.worktecket.service.IWorkTicketBasicDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 作业票基础信息Controller
 *
 * @author ruoyi
 * @date 2023-08-04
 */
@Api(tags = "APP作业票相关接口")
@RestController
@RequestMapping("/app/workticket")
public class WorkTicketForAppController extends BaseController {
    @Autowired
    private IWorkTicketBasicDataService workTicketBasicDataService;


    @ApiOperation("查询作业票信息列表")
    @GetMapping("/list")
    public TableDataInfo list(WorkTicketBasicDataDTO workTicketBasicData) {
        startPage();
        List<WorkTicketBasicDataResultDTO> list = workTicketBasicDataService.selectWorkTicketBasicDataList(workTicketBasicData);
        return getDataTable(list);
    }


    @ApiOperation("获取作业票信息详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(workTicketBasicDataService.selectWorkTicketBasicDataById(id));
    }
//废弃：从其他接口返回->获取作业票信息详细信息
//    @ApiOperation("获取作业票签字项及签字人员范围")
//    @ApiImplicitParam(name = "id", value = "作业票ID", dataType = "String", dataTypeClass = String.class)
//    @GetMapping(value = "/getSignOptionList/{id}")
//    public AjaxResult getSignOptionList(@PathVariable("id") String id) {
//        return success(workTicketBasicDataService.getSignOptionList(id));
//    }


    @ApiOperation("1、申请作业（作业预约）")
    @Log(title = "作业票基础信息", businessType = BusinessType.INSERT)
    @PostMapping
//    public AjaxResult add(@RequestBody String parms) {
    public AjaxResult add(@RequestBody WorkTicketBasicDataDTO workTicketBasicData) {

//        System.out.println("parms = " + parms);
//        JSONObject object = JSONObject.parseObject(parms);
//        System.out.println("object = " + object);
//        WorkTicketBasicDataDTO dto = JSONObject.parseObject(parms, WorkTicketBasicDataDTO.class);
//        System.out.println("dto = " + dto);
//        System.out.println("workTicketBasicData = " + workTicketBasicData);

//        if(true){
//            return AjaxResult.success("OK");
//        }
//        WorkTicketBasicDataDTO workTicketBasicData = null;
        return workTicketBasicDataService.insertWorkTicketBasicData(workTicketBasicData, false);
    }


    @ApiOperation("1.1、修改作业票信息")
    @Log(title = "作业票基础信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WorkTicketBasicDataDTO workTicketBasicData) {
        return workTicketBasicDataService.updateWorkTicketBasicData(workTicketBasicData, false);
    }

    /**
     * 2、安全交底人签字(原->设置作业票安全措施确认)
     * 负责人签字：安全交底、接受交底
     * 措施确认
     * 自定义措施
     * 第二个人签字时修改状态为：审核中（原会签中）
     * 注意：此版本废弃“20-待接受状态”状态
     *
     * @param workTicketMeasureDTO
     * @return
     */
    @ApiOperation("2、安全交底人签字(原->设置作业票安全措施确认)")
    @PostMapping("/checkWorkTicketMeasureV2")
    public AjaxResult checkWorkTicketMeasureV2(@RequestBody CheckWorkTicketMeasureDTO workTicketMeasureDTO) {
        return workTicketBasicDataService.checkWorkTicketMeasureV2(workTicketMeasureDTO);
    }



    /**
     * 2、安全交底人签字(原->设置作业票安全措施确认)
     *
     * @param workTicketMeasureDTO
     * @return
     */
    @ApiOperation("废弃2、安全交底人签字(原->设置作业票安全措施确认)")
    @PostMapping("/checkWorkTicketMeasure")
    public AjaxResult checkWorkTicketMeasure(@RequestBody CheckWorkTicketMeasureDTO workTicketMeasureDTO) {
        return workTicketBasicDataService.checkWorkTicketMeasure(workTicketMeasureDTO);
    }

    @ApiOperation("3、作业票气体分析（作业中填报）")
    @PostMapping("/addWorkTicketGasAnalysis")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "analysisLocation", value = "分析位置/部位", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "sampleTime", value = "取样时间", dataType = "Date", dataTypeClass = Date.class),
            @ApiImplicitParam(name = "workTicketId", value = "作业票主键", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "o2", value = "氧气%", dataType = "BigDecimal", dataTypeClass = BigDecimal.class),
            @ApiImplicitParam(name = "co", value = "一氧化碳%", dataType = "BigDecimal", dataTypeClass = BigDecimal.class),
            @ApiImplicitParam(name = "h2s", value = "硫化氢%", dataType = "BigDecimal", dataTypeClass = BigDecimal.class),
            @ApiImplicitParam(name = "ca", value = "可燃气%", dataType = "BigDecimal", dataTypeClass = BigDecimal.class)
    })
    public AjaxResult addWorkTicketGasAnalysis(@RequestBody WorkTicketGasAnalysisDTO workTicketGasAnalysisDTO) {
        return workTicketBasicDataService.addWorkTicketGasAnalysis(workTicketGasAnalysisDTO);
    }

    @ApiOperation("废弃3.1、接受交底人签字-作业负责人可以签（原->安全交底人签字/接受交底人签字）")
    @PostMapping("/workTicketDisclose")
    @ApiImplicitParams({
//            @ApiImplicitParam(name = "type", value = "签字类型：1安全交底人，2接受交底人", dataType = "Integer", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "signImg", value = "签字图片", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "workTicketId", value = "作业票主键", dataType = "String", dataTypeClass = String.class)
    })
    public AjaxResult workTicketDisclose(@RequestBody Map<String, String> map) {
//        Object type = map.get("type");
        Object worTicketId = map.get("workTicketId");
        Object signImg = map.get("signImg");
//        if (Objects.isNull(type) || Objects.isNull(worTicketId) || Objects.isNull(signImg)) {
//            return AjaxResult.error("参数错误");
//        }
//        return workTicketBasicDataService.workTicketDisclose((Integer) type, worTicketId.toString(), signImg.toString());
        return workTicketBasicDataService.workTicketDisclose(worTicketId.toString(), signImg.toString());
    }


    @ApiOperation("4、执行会签签字")
    @PostMapping("/workSignOptionRun")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "signImg", value = "签字图片", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "workSignOptionRunId", value = "作业票签字项ID", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "workTicketId", value = "作业票主键", dataType = "String", dataTypeClass = String.class)
    })
    public AjaxResult workSignOptionRun(@RequestBody Map<String, String> map) {
        Object workSignOptionRunId = map.get("workSignOptionRunId");
        Object worTicketId = map.get("workTicketId");
        Object signImg = map.get("signImg");
        if (Objects.isNull(workSignOptionRunId) || Objects.isNull(worTicketId) || Objects.isNull(signImg)) {
            return AjaxResult.error("参数错误");
        }
        return workTicketBasicDataService.workSignOptionRun(worTicketId.toString(), workSignOptionRunId.toString(), signImg.toString());
    }

    @ApiOperation("5、验收签字")
    @PostMapping("/acceptance")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "signImg", value = "签字图片", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "workTicketId", value = "作业票主键", dataType = "String", dataTypeClass = String.class)
    })
    public AjaxResult acceptance(@RequestBody Map<String, String> map) {
        Object worTicketId = map.get("workTicketId");
        Object signImg = map.get("signImg");
        if (Objects.isNull(worTicketId) || Objects.isNull(signImg)) {
            return AjaxResult.error("参数错误");
        }
        return workTicketBasicDataService.acceptance(worTicketId.toString(), signImg.toString());
    }


    @ApiOperation("6、获取作业票操作记录列表")
    @GetMapping(value = "/operateRecord/{id}")
    public AjaxResult getWorkTicketOperateRecord(@PathVariable("id") String workTicketId) {
        return workTicketBasicDataService.getWorkTicketOperateRecord(workTicketId);
    }


    @ApiOperation("废弃：作业负责人审核作业票")
    @PostMapping("/checkWorkTicket")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "审核类型：1驳回，2通过", dataType = "Integer", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "remark", value = "备注：驳回原因或审核通过备注", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "workTicketId", value = "作业票主键", dataType = "String", dataTypeClass = String.class)
    })
    public AjaxResult checkWorkTicket(@RequestBody Map<String, Object> map) {
        if (true) {
            return AjaxResult.error("已废弃，请转到安全交底人签字接口");
        } else {
            Object type = map.get("type");
            Object worTicketId = map.get("workTicketId");
            if (Objects.isNull(type) || Objects.isNull(worTicketId)) {
                return AjaxResult.error("参数错误");
            }
            Object remark = map.get("remark");
            return workTicketBasicDataService.checkWorkTicket((Integer) type, worTicketId.toString(), Objects.nonNull(remark) ? remark.toString() : "");
        }
    }


    @ApiOperation("废弃：设置作业票安全措施编制人")
    @PostMapping("/checkMeasuresComplementUser")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "measuresComplementUser", value = "编制人ID", dataType = "Integer", dataTypeClass = Integer.class),
//            @ApiImplicitParam(name = "measuresComplementUserSignImg", value = "编制人签字图片", dataType = "String", dataTypeClass = String.class),
//            @ApiImplicitParam(name = "workTicketId", value = "作业票主键", dataType = "String", dataTypeClass = String.class)
//    })
    public AjaxResult checkMeasuresComplementUser(@RequestBody CheckWorkTicketMeasureDTO workTicketMeasureDTO) {
        if (true) {
            return AjaxResult.error("已废弃，请转到安全交底人签字接口");
        } else {

            return workTicketBasicDataService.checkMeasuresComplementUser(workTicketMeasureDTO);
        }
    }
}
