package com.fufan.mtsaas.modules.sysmgr.sydept.controller;


import com.alibaba.fastjson.JSONObject;
import com.fufan.mtsaas.modules.common.bo.request.*;
import com.fufan.mtsaas.modules.common.bo.response.RequestPojo;
import com.fufan.mtsaas.modules.common.bo.response.Result;
import com.fufan.mtsaas.modules.sysmgr.sydept.bo.request.DeptQueryRequest;
import com.fufan.mtsaas.modules.sysmgr.sydept.bo.request.DeptSaveRequest;
import com.fufan.mtsaas.modules.sysmgr.sydept.model.SYDepVer;
import com.fufan.mtsaas.modules.sysmgr.sydept.model.SYDept;
import com.fufan.mtsaas.modules.sysmgr.sydept.service.SYDeptSvc;
import com.fufan.mtsaas.modules.common.bo.request.DataRequest;
import com.fufan.mtsaas.modules.common.bo.request.ResultPack;
import com.fufan.mtsaas.modules.sysmgr.sydept.bo.request.DeptQueryRequest;
import com.fufan.mtsaas.modules.sysmgr.sydept.bo.request.DeptSaveRequest;
import com.fufan.mtsaas.modules.sysmgr.sydept.model.SYDept;
import com.fufan.mtsaas.modules.common.bo.response.RequestPojo;
import com.fufan.mtsaas.modules.common.bo.response.Result;
import com.fufan.mtsaas.modules.sysmgr.sydept.service.SYDeptSvc;
import com.fufan.mtsaas.util.JSonUtil;
import com.fufan.mtsaas.util.ParamUtil;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * <p>
 * 
 * </p>
 *
 * @author Liby
 * @since 2019-08-19
 */
@Slf4j
@Api(tags = "MTSaas：系统管理-部门信息模块", description = "SYDept对象接口")
@RestController
@RequestMapping("/SYS/SYDept")
public class SYDeptCtrl {
    @Autowired
    private SYDeptSvc targetService;

    /**
     * 分页查询
     * */
    @ApiOperation(value = "分页查询所有，根据需要传入分页参数：pageSize每页数量(默认为10)，currentPage当前页数(默认为1)", httpMethod = "POST", produces = "application/json")
    @PostMapping(value ="/doQueryPage")
    public Result<SYDept> doQueryPage(@RequestBody @ApiIgnore DataRequest dataRequest, DeptQueryRequest request){
        ResultPack resultPack = new ResultPack();
        RequestPojo requestPojo = ParamUtil.doChange();
        JSONObject mapdata = dataRequest.getData().getParam();
        DeptQueryRequest map = JSonUtil.toBean(mapdata.toJSONString(), DeptQueryRequest.class);

        return resultPack.doResultPack(targetService.doQueryPage(map) ,requestPojo.getIntfId(), requestPojo.getAppKey());
    }

    /**
     * 获取所有的有效部门
     * */
    @ApiOperation(value = "获取所有的有效部门", httpMethod = "POST", produces = "application/json")
    @PostMapping(value ="/doAll")
    public Result<SYDept> doAll(@RequestBody @ApiIgnore DataRequest dataRequest, DeptQueryRequest request){
        ResultPack resultPack = new ResultPack();
        RequestPojo requestPojo = ParamUtil.doChange();
        JSONObject mapdata = dataRequest.getData().getParam();
        DeptQueryRequest map = JSonUtil.toBean(mapdata.toJSONString(), DeptQueryRequest.class);
        // 一般不会出现这么多部门，所以这里写个很大的值，用来获取所有部门
        map.setCurrentPage(1);
        map.setPageSize(10000);
        return resultPack.doResultPack(targetService.doQueryPage(map) ,requestPojo.getIntfId(), requestPojo.getAppKey());
    }

    /**
     * 新增
     * */
    @ApiOperation(value = "新增", httpMethod = "POST", produces = "application/json")
    @RequestMapping(value = "/doSave", method = RequestMethod.POST)
    public Result<SYDept> doSaveMap(@RequestBody @ApiIgnore DataRequest dataRequest, DeptSaveRequest request, @ApiIgnore HttpSession session){
        ResultPack resultPack = new ResultPack();
        RequestPojo requestPojo = ParamUtil.doChange();
        JSONObject mapdata = dataRequest.getData().getParam();
        DeptSaveRequest map = JSonUtil.toBean(mapdata.toJSONString(), DeptSaveRequest.class);

        return resultPack.doResultPack(targetService.doSaveMap(map, requestPojo), requestPojo.getIntfId(), requestPojo.getAppKey());
    }

    /**
     * 修改
     * */
    @ApiOperation(value = "修改", httpMethod = "PUT", produces = "application/json")
    @RequestMapping(value = "/doUpdate", method = RequestMethod.PUT)
    public Result<SYDept> doUpdateMap(@RequestBody @ApiIgnore DataRequest dataRequest, DeptSaveRequest request, @ApiIgnore HttpSession session) {
        ResultPack resultPack = new ResultPack();
        RequestPojo requestPojo = ParamUtil.doChange();
        JSONObject mapdata = dataRequest.getData().getParam();
        DeptSaveRequest map = JSonUtil.toBean(mapdata.toJSONString(), DeptSaveRequest.class);

        return resultPack.doResultPack(targetService.doUpdateMap(map, requestPojo), requestPojo.getIntfId(), requestPojo.getAppKey());
    }

    /**
     * 删除数据
     * */
    @ApiOperation(value = "删除数据", httpMethod = "DELETE", produces = "application/json")

    @RequestMapping(value = "/doDelete", method = RequestMethod.DELETE)
    public Result doDelete(@RequestBody @ApiIgnore DataRequest dataRequest, DeleteRequest request) {
        ResultPack resultPack = new ResultPack();
        RequestPojo requestPojo = ParamUtil.doChange();
        JSONObject mapdata = dataRequest.getData().getParam();
        DeleteRequest map = JSonUtil.toBean(mapdata.toJSONString(), DeleteRequest.class);

        return resultPack.doResultPack(targetService.doDelete(map, requestPojo), requestPojo.getIntfId(), requestPojo.getAppKey());
    }

    /**
     * 查询部门结构
     * */
    @ApiOperation(value = "查询部门结构", httpMethod = "GET", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "公司标识", name = "CompanyID", dataType = "string", required = true),
            @ApiImplicitParam(value = "部门代码标识", name = "SubID", dataType = "string", required = false),
            @ApiImplicitParam(value = "版本号", name = "VerNo", dataType = "string", required = false)
    })
    @PostMapping(value = "/doQueryStructure")
    public Result<SYDept> doQueryStructure(@RequestBody @ApiIgnore DataRequest dataRequest, DeptQueryRequest request) {
        ResultPack resultPack = new ResultPack();
        RequestPojo requestPojo = ParamUtil.doChange();
        JSONObject mapdata = dataRequest.getData().getParam();
        DeptQueryRequest map1 = JSonUtil.toBean(mapdata.toJSONString(), DeptQueryRequest.class);
        Map map = JSonUtil.stringToCollect(mapdata.toJSONString());

        return resultPack.doResultPack(targetService.doQueryStructure(map),requestPojo.getIntfId(),requestPojo.getAppKey());
    }
}
