package com.gyee.frame.controller.ticket;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gyee.frame.common.dataSources.DataSource;
import com.gyee.frame.common.dataSources.DataSourceType;
import com.gyee.frame.common.domain.AjaxResult;
import com.gyee.frame.model.ticket.Wosafemeasure;
import com.gyee.frame.model.ticket.Wosafetybaffle;
import com.gyee.frame.service.ticket.WosafemeasureService;
import com.gyee.frame.service.ticket.WosafetybaffleService;
import com.gyee.frame.util.ticket.TicketUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 风机检修工作票安全措施
 */
@RestController
@RequestMapping("/wind/safe/measure")
public class WindSafeMeasureController {

    @Autowired
    WosafemeasureService wosafemeasureService;
    @Autowired
    WosafetybaffleService wosafetybaffleService;

    /**
     * 根据主单据的 wonum 关联编号查询安全措施
     *
     * @param wonum
     * @return
     */
    @DataSource(value = DataSourceType.TICKET)
    @GetMapping(value = "list")
    public AjaxResult getSafeMeasure(@RequestParam("wonum") String wonum) {
        // 运行执行、检修自理安全措施
        List<Wosafemeasure> yxzxaqcs = wosafemeasureService.getListSafeMeasure(wonum, "YXZXAQCS");
        List<Wosafemeasure> jxzlaqcs = wosafemeasureService.getListSafeMeasure(wonum, "JXZLAQCS");

        // 应设遮栏、应挂标示牌（注明装设地点）、应使用的安全工器具、应佩戴的防护用品
        List<Wosafetybaffle> protectcs = wosafetybaffleService.getListOfProtectMeasure(wonum, "WINDELECZSP");

        // 工作位置及相邻区域内的带电部位、孔洞、旋转部件、有限空间作业、免爬器使用等
        List<Wosafemeasure> tooluse = wosafemeasureService.getListSafeMeasure(wonum, "AQZYSX");

        Map<String, Object> maps = new HashMap<>();
        maps.put("yxzxaqcs", yxzxaqcs);
        maps.put("jxzlaqcs", jxzlaqcs);
        maps.put("protectcs", protectcs);
        maps.put("tooluse", tooluse);

        return AjaxResult.successData(maps);
    }


    /**
     * 编辑安全措施
     *
     * @param data
     * @return
     */
    @DataSource(value = DataSourceType.TICKET)
    @PostMapping(value = "edit")
    public AjaxResult editSafeMeasure(@RequestParam("data") String data, @RequestParam("type") String type) {
        if (type.isEmpty())
            return AjaxResult.error(4004, "数据更新失败");

        if (type.equals("jxzlaqcs") || type.equals("yxzxaqcs") || type.equals("tooluse")) {
            JSONObject jsonObject = JSONObject.parseObject(data);
            Wosafemeasure measure = JSON.toJavaObject(jsonObject, Wosafemeasure.class);

            boolean result = wosafemeasureService.updateById(measure);
            if (!result)
                return AjaxResult.error(4004, "数据更新失败");
        }

        if (type.equals("protectcs")) {
            JSONObject jsonObject = JSONObject.parseObject(data);
            Wosafetybaffle measure = JSON.toJavaObject(jsonObject, Wosafetybaffle.class);

            boolean result = wosafetybaffleService.updateById(measure);
            if (!result)
                return AjaxResult.error(4004, "数据更新失败");
        }

        return AjaxResult.success("数据更新成功");
    }

    /**
     * 根据 wonum 新增安全措施
     *
     * @param data
     * @return
     */
    @DataSource(value = DataSourceType.TICKET)
    @PostMapping(value = "create")
    public AjaxResult createSafeMeasure(@RequestParam("data") String data, @RequestParam("type") String type) {
        if (type.isEmpty())
            return AjaxResult.error(4004, "数据插入失败");

        if (type.equals("jxzlaqcs") || type.equals("yxzxaqcs") || type.equals("tooluse")) {
            JSONObject jsonObject = JSONObject.parseObject(data);
            Wosafemeasure measure = JSON.toJavaObject(jsonObject, Wosafemeasure.class);
            wosafemeasureService.insertSafeMeasure(measure);
        }

        if (type.equals("protectcs")) {
            JSONObject jsonObject = JSONObject.parseObject(data);
            Wosafetybaffle measure = JSON.toJavaObject(jsonObject, Wosafetybaffle.class);
            wosafetybaffleService.insertSafeMeasure(measure);
        }

        return AjaxResult.success("数据添加成功");
    }

    /**
     * 批量删除安全措施
     *
     * @param data
     * @return
     */
    @DataSource(value = DataSourceType.TICKET)
    @PostMapping(value = "delete")
    public AjaxResult deleteSafeMeasure(@RequestParam("list") String data) {
        Map map = TicketUtil.formatSafeMeasureWind(data);
        List listS = (List) map.get("safemeasure");
        List listW = (List) map.get("safetybaffle");

        if (listS != null && listS.size() > 0) {
            int code = wosafemeasureService.deleteSafeMeasure(listS);
            if (code <= 0)
                return AjaxResult.error(4004, "数据删除失败");
        }
        if (listW != null && listW.size() > 0) {
            int code = wosafetybaffleService.deleteSafeMeasure(listW);
            if (code <= 0)
                return AjaxResult.success("数据删除失败");
        }

        return AjaxResult.success("数据删除成功");
    }
}
