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

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.casic.missiles.core.application.context.AbstractPermissionContext;
import com.casic.missiles.core.base.controller.BaseController;
import com.casic.missiles.core.page.PageFactory;
import com.casic.missiles.model.exception.ServiceException;
import com.casic.missiles.model.exception.enums.CoreExceptionEnum;
import com.casic.missiles.model.response.ResponseData;
import com.casic.missiles.model.response.SuccessResponseData;
import com.casic.missiles.modular.system.model.DeptPermission;
import com.casic.missiles.modular.system.service.IDeptPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
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;
    @Resource
    private AbstractPermissionContext service;

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

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

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

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

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

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

    @GetMapping(value = "/getAuthorByDept")
    @ResponseBody
    public Object getAuthorByDept() {
        SuccessResponseData resultData = new SuccessResponseData();
        resultData.setCode(200);
        resultData.setMessage("查询详情成功");

        //递归查询组织相关配置数据
        DeptPermission deptPermission = deptPermissionService.selectDeptPermission(service.getAuthService().getLoginUser().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;
    }

    @GetMapping(value = "/getAreaByDept")
    @ResponseBody
    public Object getAreaByDept(Long deptId) {
        if (deptId == null) {
            throw new ServiceException(CoreExceptionEnum.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.getById(deptPermissionId));
    }
}
