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.*;
import com.gyee.frame.service.ticket.*;
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("/elec1/safe/measure")
public class Elec1SafeMeasureController {

    @Autowired
    WosafetyService wosafetyService;
    @Autowired
    WoearthlineService woearthlineService;
    @Autowired
    WosafetybaffleService wosafetybaffleService;
    @Autowired
    WosafemeasureService wosafemeasureService;

    /**
     * 根据主单据的 wonum 关联编号查询安全措施
     *
     * @param wonum
     * @return
     */
    @DataSource(value = DataSourceType.TICKET)
    @GetMapping(value = "list")
    public AjaxResult getSafeMeasure(@RequestParam("wonum") String wonum) {
        // 断路器（开关）和隔离开关（刀闸）
        List<Wosafety> dlqglkg = wosafetyService.getListSafeMeasure(wonum);
        // 接地线和接地刀闸
        List<Woearthline> jdxjddz = woearthlineService.getListSafeMeasure(wonum);
        // 遮拦、标识牌
        List<Wosafetybaffle> protectcs = wosafetybaffleService.getListOfProtectMeasure(wonum, "遮拦、标示牌");
        // 注意事项及安全措施
        List<Wosafemeasure> attention = wosafemeasureService.getListSafeMeasure(wonum, "ELEC1AQZYSX");

        Map<String, Object> maps = new HashMap<>();
        maps.put("dlqglkg", dlqglkg);
        maps.put("jdxjddz", jdxjddz);
        maps.put("protectcs", protectcs);
        maps.put("attention", attention);

        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("dlqglkg")) {
            JSONObject jsonObject = JSONObject.parseObject(data);
            Wosafety measure = JSON.toJavaObject(jsonObject, Wosafety.class);

            boolean result = wosafetyService.updateById(measure);
            if (!result)
                return AjaxResult.error(4004, "数据更新失败");
        } else if (type.equals("jdxjddz")) {
            JSONObject jsonObject = JSONObject.parseObject(data);
            Woearthline measure = JSON.toJavaObject(jsonObject, Woearthline.class);

            boolean result = woearthlineService.updateById(measure);
            if (!result)
                return AjaxResult.error(4004, "数据更新失败");
        } else 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, "数据更新失败");
        } else if (type.equals("attention")) {
            JSONObject jsonObject = JSONObject.parseObject(data);
            Wosafemeasure measure = JSON.toJavaObject(jsonObject, Wosafemeasure.class);

            boolean result = wosafemeasureService.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("dlqglkg")) {
            JSONObject jsonObject = JSONObject.parseObject(data);
            Wosafety measure = JSON.toJavaObject(jsonObject, Wosafety.class);
            wosafetyService.insertSafeMeasure(measure);
        } else if (type.equals("jdxjddz")) {
            JSONObject jsonObject = JSONObject.parseObject(data);
            Woearthline measure = JSON.toJavaObject(jsonObject, Woearthline.class);
            woearthlineService.insertSafeMeasure(measure);
        } else if (type.equals("protectcs")) {
            JSONObject jsonObject = JSONObject.parseObject(data);
            Wosafetybaffle measure = JSON.toJavaObject(jsonObject, Wosafetybaffle.class);
            wosafetybaffleService.insertSafeMeasure(measure);
        } else if (type.equals("attention")) {
            JSONObject jsonObject = JSONObject.parseObject(data);
            Wosafemeasure measure = JSON.toJavaObject(jsonObject, Wosafemeasure.class);
            wosafemeasureService.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.formatSafeMeasureElec1(data);
        List listD = (List) map.get("dlqglkg");
        List listJ = (List) map.get("jdxjddz");
        List listP = (List) map.get("protectcs");
        List listA = (List) map.get("attention");

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

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