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.Fjyk;
import com.gyee.frame.model.ticket.Ibsidgen;
import com.gyee.frame.service.ticket.FjykService;
import com.gyee.frame.service.ticket.IbsidgenService;
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;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author chenmh
 * @since 2020-07-26
 */
@RestController
@RequestMapping("/risk/measure")
public class FjykController {

    @Autowired
    FjykService fjykService;


    /**
     * 根据主单据的 wonum 关联编号查询风险预控措施
     *
     * @param wonum
     * @return
     */
    @DataSource(value = DataSourceType.TICKET)
    @GetMapping(value = "list")
    public AjaxResult getRiskMeasure(@RequestParam("wonum") String wonum) {
        // 工作负责人风险预控
        List<Fjyk> principal = fjykService.getListOfRiskMeasure(wonum, "PRINCIPAL");
        // 许可人风险预控
        List<Fjyk> fiatuser = fjykService.getListOfRiskMeasure(wonum, "FIATUSER");

        Map<String, List> maps = new HashMap<>();
        maps.put("principalfxyk", principal);
        maps.put("fiatuserfxyk", fiatuser);

        return AjaxResult.successData(maps);
    }

    /**
     * 根据 id 获取风险预控详情
     *
     * @param id
     * @return
     */
    @DataSource(value = DataSourceType.TICKET)
    @GetMapping(value = "item")
    public AjaxResult getRiskItem(@RequestParam("id") String id) {
        Fjyk item = fjykService.getById(id);
        if (item == null)
            return AjaxResult.error(4004, "数据请求异常");

        return AjaxResult.successData(item);
    }


    /**
     * 根据 id 删除风险预控
     *
     * @param id
     * @return
     */
    @DataSource(value = DataSourceType.TICKET)
    @PostMapping(value = "delete")
    public AjaxResult deleteRiskMeasure(@RequestParam("id") String id) {
        boolean result = fjykService.removeById(id);
        if (!result)
            return AjaxResult.error(4004, "数据删除失败");

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

    /**
     * 编辑风险预控
     *
     * @param data
     * @return
     */
    @DataSource(value = DataSourceType.TICKET)
    @PostMapping(value = "edit")
    public AjaxResult editRiskMeasure(@RequestParam("data") String data) {
        JSONObject jsonObject = JSONObject.parseObject(data);
        Fjyk fjyk = JSON.toJavaObject(jsonObject, Fjyk.class);

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

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

    /**
     * 根据 wonum 新增风险预控
     *
     * @param data
     * @return
     */
    @DataSource(value = DataSourceType.TICKET)
    @PostMapping(value = "create")
    public AjaxResult createRiskMeasure(@RequestParam("data") String data) {
        JSONObject jsonObject = JSONObject.parseObject(data);
        Fjyk fjyk = JSON.toJavaObject(jsonObject, Fjyk.class);
        if (fjyk == null)
            return AjaxResult.error(4004, "数据插入失败");

        fjykService.insertRiskMeasure(fjyk);

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