package com.casic.missiles.modular.system.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.casic.missiles.modular.system.model.DeptPermission;
import com.casic.missiles.modular.system.service.IDeptPermissionService;
import com.casic.missiles.core.base.controller.BaseController;
import com.casic.missiles.core.base.response.ResponseData;
import com.casic.missiles.core.base.response.SuccessResponseData;
import com.casic.missiles.core.common.constant.factory.PageFactory;
import com.casic.missiles.core.common.exception.BizExceptionEnum;
import com.casic.missiles.core.common.service.ICommonPermissionService;
import com.casic.missiles.core.exception.GunsException;
import com.casic.missiles.core.util.ToolUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 组织业务支持配置控制器
 *
 * @author dev
 * @Date 2019-05-28 15:08:56
 */
@Controller
@RequestMapping("/config")
public class DeptPermissionController extends BaseController {


    @Autowired
    private IDeptPermissionService deptPermissionService;
    @Autowired
    private ICommonPermissionService service;

    /**
     * 获取组织业务支持配置列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        return deptPermissionService.selectList(null);
    }

    /**
     * 获取组织业务支持配置分页列表
     */
    @RequestMapping(value = "/listPage")
    @ResponseBody
    public Object listPage(String condition) {
        Page<DeptPermission> page = new PageFactory<DeptPermission>().defaultPage();
        EntityWrapper<DeptPermission> query = new EntityWrapper<>();
        page = deptPermissionService.selectPage(page, query);
        return super.packForBT(page);
    }

    /**
     * 新增组织业务支持配置
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(DeptPermission deptPermission) {
        deptPermissionService.insert(deptPermission);
        return ResponseData.success();
    }

    /**
     * 删除组织业务支持配置
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam String deptPermissionId) {
        deptPermissionService.deleteById(deptPermissionId);
        return ResponseData.success();
    }

    /**
     * 批量删除
     */
    @RequestMapping(value = "/batchDelete")
    @ResponseBody
    public Object delete(@RequestParam("deptPermissionIds") List<String> deptPermissionIds) {
        deptPermissionService.deleteBatchIds(deptPermissionIds);
        return ResponseData.success();
    }

    /**
     * 修改组织业务支持配置
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(DeptPermission deptPermission) {
        deptPermissionService.updateById(deptPermission);
        return ResponseData.success();
    }

    @ApiOperation("获取部门业务权限")
    @GetMapping(value = "/getAuthorByDept")
    @ResponseBody
    public Object getAuthorByDept() {
        SuccessResponseData resultData = new SuccessResponseData();
        resultData.setCode(200);
        resultData.setMessage("查询详情成功");

        //递归查询组织相关配置数据
        DeptPermission deptPermission = deptPermissionService.selectDeptPermission(service.getCurrLoginUser().getDeptId());
        if (deptPermission != null) {
            deptPermission.setWellTypes(JSON.parseObject(deptPermission.getWellType(), List.class));
            deptPermission.setDeviceTypes(JSON.parseObject(deptPermission.getDeviceType(), List.class));
            deptPermission.setCommunications(JSON.parseObject(deptPermission.getCommunication(), List.class));
        }
        resultData.setData(deptPermission);
        return resultData;
    }

    @ApiOperation("根据部门查找其默认区域")
    @GetMapping(value = "/getAreaByDept")
    @ResponseBody
    public Object getAreaByDept(Long deptId) {
        if (ToolUtil.isEmpty(deptId)) {
            throw new GunsException(BizExceptionEnum.REQUEST_NULL);
        }
        SuccessResponseData resultData = new SuccessResponseData();
        resultData.setCode(200);
        resultData.setMessage("查询详情成功");
        //递归查询组织相关配置数据
        resultData.setData(deptPermissionService.selectDeptPermission(deptId));
        return resultData;
    }

    /**
     * 组织业务支持配置详情
     */
    @RequestMapping(value = "/detail/{deptPermissionId}")
    @ResponseBody
    public Object detail(@PathVariable("deptPermissionId") String deptPermissionId) {
        return ResponseData.success(deptPermissionService.selectById(deptPermissionId));
    }
}
