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.Wosaferepair;
import com.gyee.frame.model.ticket.Wosaferun;
import com.gyee.frame.service.ticket.WosaferepairService;
import com.gyee.frame.service.ticket.WosaferunService;
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("/fire1/safe/measure")
public class Fire1SafeMeasureController {

    @Autowired
    WosaferunService wosaferunService;
    @Autowired
    WosaferepairService wosaferepairService;


    /**
     * 根据主单据的 wonum 关联编号查询安全措施
     *
     * @param wonum
     * @return
     */
    @DataSource(value = DataSourceType.TICKET)
    @GetMapping(value = "list")
    public AjaxResult getSafeMeasure(@RequestParam("wonum") String wonum) {
        // 运行部门应采取的安全措施
        List<Wosaferun> saferun = wosaferunService.getListSafeMeasure(wonum);
        // 动火部门应采取的安全措施
        List<Wosaferepair> saferepair = wosaferepairService.getListSafeMeasure(wonum);

        Map<String, Object> maps = new HashMap<>();
        maps.put("saferun", saferun);
        maps.put("saferepair", saferepair);

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

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

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

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

            boolean result = wosaferepairService.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.formatSafeMeasureFire1(data);
        List listC = (List) map.get("saferun");
        List listS = (List) map.get("saferepair");

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

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


