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("/line1/safe/measure")
public class Line1SafeMeasureController {

    @Autowired
    WosafetyService wosafetyService;
    @Autowired
    WohaveeleService wohaveeleService;
    @Autowired
    WoearthlineService woearthlineService;
    @Autowired
    WoadditionsafemeasureService woadditionsafemeasureService;

    /**
     * 根据主单据的 wonum 关联编号查询安全措施
     *
     * @param wonum
     * @return
     */
    @DataSource(value = DataSourceType.TICKET)
    @GetMapping(value = "list")
    public AjaxResult getSafeMeasure(@RequestParam("wonum") String wonum) {
        // 应拉开的隔离开关、断路器、应停电的范围及应设遮栏和标识牌
        List<Wosafety> glkgdlq = wosafetyService.getListSafeMeasure(wonum);
        // 补充安全措施
        List<Woadditionsafemeasure> safety = woadditionsafemeasureService.getListSafeMeasure(wonum);
        // 保留的带电线路或带电设备
        List<Wohaveele>  hoveele = wohaveeleService.getListSafeMeasure(wonum);
        // 应挂的接地线、应合的接地刀闸
        List<Woearthline>  earthline = woearthlineService.getListSafeMeasure(wonum);

        Map<String, Object> maps = new HashMap<>();
        maps.put("glkgdlq", glkgdlq);
        maps.put("safety", safety);
        maps.put("hoveele", hoveele);
        maps.put("earthline", earthline);

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

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

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

            boolean result = woearthlineService.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("glkgdlq")) {
            JSONObject jsonObject = JSONObject.parseObject(data);
            Wosafety measure = JSON.toJavaObject(jsonObject, Wosafety.class);
            int id = wosafetyService.selectMaxId();
            measure.setId((double) (id + 1));

            boolean result = wosafetyService.save(measure);
            if (!result)
                return AjaxResult.error(4004, "数据插入失败");
        }else if (type.equals("safety")){
            JSONObject jsonObject = JSONObject.parseObject(data);
            Woadditionsafemeasure measure = JSON.toJavaObject(jsonObject, Woadditionsafemeasure.class);
            int id = woadditionsafemeasureService.selectMaxId();
            measure.setId((double) (id + 1));

            boolean result = woadditionsafemeasureService.save(measure);
            if (!result)
                return AjaxResult.error(4004, "数据插入失败");
        }else if (type.equals("hoveele")) {
            JSONObject jsonObject = JSONObject.parseObject(data);
            Wohaveele measure = JSON.toJavaObject(jsonObject, Wohaveele.class);
            int id = wohaveeleService.selectMaxId();
            measure.setId((double) (id + 1));

            boolean result = wohaveeleService.save(measure);
            if (!result)
                return AjaxResult.error(4004, "数据插入失败");
        }else if (type.equals("earthline")){
            JSONObject jsonObject = JSONObject.parseObject(data);
            Woearthline measure = JSON.toJavaObject(jsonObject, Woearthline.class);
            int id = woearthlineService.selectMaxId();
            measure.setId((double) (id + 1));

            boolean result = woearthlineService.save(measure);
            if (!result)
                return AjaxResult.error(4004, "数据插入失败");
        }

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


    /**
     * 批量删除安全措施
     *
     * @param data
     * @return
     */
    @DataSource(value = DataSourceType.TICKET)
    @PostMapping(value = "delete")
    public AjaxResult deleteSafeMeasure(@RequestParam("list") String data) {
        Map map = TicketUtil.formatSafeMeasureLine1(data);
        List listG = (List) map.get("glkgdlq");
        List listS = (List) map.get("safety");
        List listH = (List) map.get("hoveele");
        List listE = (List) map.get("earthline");

        if (listG != null && listG.size() > 0) {
            int code = wosafetyService.deleteSafeMeasure(listG);
            if (code <= 0)
                return AjaxResult.error(4004, "数据删除失败");
        }
        if (listS != null && listS.size() > 0) {
            int code = woadditionsafemeasureService.deleteSafeMeasure(listS);
            if (code <= 0)
                return AjaxResult.success("数据删除失败");
        }
        if (listH != null && listH.size() > 0) {
            int code = wohaveeleService.deleteSafeMeasure(listH);
            if (code <= 0)
                return AjaxResult.error(4004, "数据删除失败");
        }
        if (listE != null && listE.size() > 0) {
            int code = woearthlineService.deleteSafeMeasure(listE);
            if (code <= 0)
                return AjaxResult.success("数据删除失败");
        }

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