package com.dxhy.admin.sys.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.dxhy.admin.sys.entity.SysTabulateEntity;
import com.dxhy.admin.sys.service.SysTabulateDeptService;
import com.dxhy.admin.sys.service.SysTabulateService;
import com.dxhy.common.aspect.SysLog;
import com.dxhy.common.controller.AbstractController;
import com.dxhy.common.utils.PageUtils;
import com.dxhy.common.utils.R;

/**
 * @author jiaohongyang（焦红阳）
 * @date 2020-09-22
 */
@SuppressWarnings("AlibabaUndefineMagicConstant")
@RestController
@RequestMapping("/tabulate")
public class SysTabulateController extends AbstractController {
    @Resource
    private SysTabulateService sysTabulateService;
    @Resource
    private SysTabulateDeptService sysTabulateDeptService;

    /**
     * 保存显示列表信息
     *
     * @param datalist
     *            显示列表信息
     * @return 返回信息
     */
    @RequestMapping("/save")
    @SysLog("显示列表保存")
    public ResponseEntity<?> save(@RequestBody String datalist) {
        int code = sysTabulateService.saveTablate(datalist);
        return getSaveResponseEntity(code);
    }

    @NotNull
    private ResponseEntity<?> getSaveResponseEntity(int code) {
        if (code == 1) {
            return ResponseEntity.ok(R.ok("显示列表保存成功"));
        } else if (code == 2) {
            return ResponseEntity.ok(R.error("显示列表保存失败，存在相同显示列表"));
        } else if (code == 3) {
            return ResponseEntity.ok(R.error("显示列表保存失败"));
        } else if (code == 4) {
            return ResponseEntity.ok(R.error("传入显示列表空值"));
        } else if (code == 5) {
            return ResponseEntity.ok(R.error("只有中心企业才可以批量分配添加显示列表"));
        } else {
            return ResponseEntity.ok(R.error("显示列表保存失败"));
        }
    }

    /**
     * 修改显示列表信息
     *
     * @param sysTabulateEntity
     *            显示列表信息
     * @return 返回信息
     */
    @RequestMapping("/update")
    @SysLog("显示列表修改")
    public ResponseEntity<?> update(@RequestBody SysTabulateEntity sysTabulateEntity) {
        int code = sysTabulateService.updateTablate(sysTabulateEntity);
        return getSaveResponseEntity(code);
    }

    /**
     * 删除显示列表信息
     *
     * @param params
     *            列表id
     * @return 返回信息
     */
    @RequestMapping("/delete")
    @SysLog("显示列表删除")
    public ResponseEntity<?> delete(@RequestBody Map<String, Object> params) {
        int code = sysTabulateService.deleteTabulate(Long.parseLong((String)params.get("tabulateId")));
        if (code == 1) {
            return ResponseEntity.ok(R.ok("删除显示列表成功"));
        } else if (code == 2) {
            return ResponseEntity.ok(R.error("删除显示列表失败"));
        } else {
            return ResponseEntity.ok(R.error("删除显示列表失败"));
        }
    }

    /**
     * 删除显示列表信息
     *
     * @param params
     *            入参
     * @return 返回信息
     */
    @RequestMapping("/deleteTabulateDept")
    @SysLog("角色分配显示列表删除")
    public ResponseEntity<?> deleteTabulateDept(@RequestBody Map<String, Object> params) {
        int code = sysTabulateDeptService.deleteTabulateDept(params);
        if (code == 0) {
            return ResponseEntity.ok(R.ok("删除显示列表成功"));
        } else if (code == 1) {
            return ResponseEntity.ok(R.error("删除显示列表失败"));
        } else {
            return ResponseEntity.ok(R.error("删除显示列表失败"));
        }
    }

    /**
     * 修改显示列表信息
     *
     * @param params
     *            入参
     * @return 返回
     */
    @RequestMapping("/updateTabulateDept")
    @SysLog("角色分配显示列表修改")
    public ResponseEntity<?> updateTabulateDept(@RequestBody Map<String, Object> params) {
        int code = sysTabulateDeptService.updateTabulateDept(params);
        if (code == 0) {
            return ResponseEntity.ok(R.ok("显示列表修改成功"));
        } else if (code == 1) {
            return ResponseEntity.ok(R.error("显示列表修改失败"));
        } else {
            return ResponseEntity.ok(R.error("显示列表修改失败"));
        }
    }

    /**
     * 根据 菜单id查询显示列表信息
     *
     * @param params
     *            菜单id
     * @return 显示列表信息
     */
    @RequestMapping("/select")
    public ResponseEntity<?> selectMenuId(@RequestBody Map<String, Object> params) {
        List<SysTabulateEntity> tablate =
            sysTabulateService.getTablate(null, (String)params.get("menuId"), null, null, false);
        return ResponseEntity.ok(R.ok().put("list", tablate));
    }

    /**
     * 分页查询结果列表信息
     *
     * @param params
     *            分页信息
     * @return 返回信息
     */
    @RequestMapping("/list")
    public ResponseEntity<?> list(@RequestBody Map<String, Object> params) {
        PageUtils page = sysTabulateService.queryPage(params);

        return ResponseEntity.ok(R.ok().put("page", page));
    }

    /**
     * 查询已分配权限显列表信息
     *
     * @param params
     *            入参
     * @return 返回
     */
    @RequestMapping("/deptTabulateList")
    public ResponseEntity<?> deptTabulateList(@RequestBody Map<String, Object> params) {
        PageUtils page = sysTabulateDeptService.queryPage(params);
        return ResponseEntity.ok(R.ok().put("page", page));
    }

    /**
     * 保存显示列表关联集团信息
     *
     * @param datalist
     *            显示列表信息
     * @return 返回信息
     */
    @RequestMapping("/saveTabulateDept")
    @SysLog("角色分配显示列表保存")
    public ResponseEntity<?> saveTabulateDept(@RequestBody String datalist) {
        int code = sysTabulateDeptService.saveTablateDeptJson(datalist);
        return getSaveResponseEntity(code);
    }

    /**
     * 查询未分配的显示列表信息
     *
     * @param params
     *            入参
     * @return 显示列表信息
     */
    @RequestMapping("/getNotTabulateDept")
    public ResponseEntity<?> getNotTabulateDept(@RequestBody Map<String, Object> params) {
        List<SysTabulateEntity> tablateNotDept = sysTabulateDeptService.getTablateNotDept(params);
        return ResponseEntity.ok(R.ok().put("data", tablateNotDept));
    }

    /**
     * 根据集团编码刷新缓存，如集团编码为空，则刷新中心企业缓存
     *
     * @param params
     *            入参
     * @return 返回信息
     */
    @RequestMapping("/refreshCache")
    public ResponseEntity<?> refreshCache(@RequestBody Map<String, Object> params) {
        int code = sysTabulateDeptService.refreshCache((String)params.get("company"));
        return getResponseEntity(code);
    }

    @NotNull
    private ResponseEntity<?> getResponseEntity(int code) {
        if (code == 0) {
            return ResponseEntity.ok(R.ok("刷新缓存成功"));
        } else if (code == 1) {
            return ResponseEntity.ok(R.error("刷新缓存失败，没有需要刷新缓存数据"));
        } else if (code == 2) {
            return ResponseEntity.ok(R.error("刷新缓存失败"));
        } else {
            return ResponseEntity.ok(R.error("刷新缓存失败"));
        }
    }

    /**
     * 批量刷新显示列表缓存
     *
     * @return 返回信息
     */
    @RequestMapping("/listRefreshCache")
    public ResponseEntity<?> listRefreshCache() {
        int code = sysTabulateDeptService.listRefreshCache();
        return getResponseEntity(code);
    }

    /**
     * 取消勾选框
     *
     * @param params
     *            入参
     * @return 返回
     */
    @RequestMapping("/cancelSelection")
    public ResponseEntity<?> cancelSelection(@RequestBody Map<String, Object> params) {
        boolean cancel = sysTabulateService.cancel(params, "selection");
        if (cancel) {
            return ResponseEntity.ok(R.ok("取消勾选成功"));
        } else {
            return ResponseEntity.ok(R.error("取消勾选失败"));
        }
    }

    /**
     * 取消本地序号
     *
     * @param params
     *            入参
     * @return 返回
     */
    @RequestMapping("/cancelIndexType")
    public ResponseEntity<?> cancelIndexType(@RequestBody Map<String, Object> params) {
        boolean cancel = sysTabulateService.cancel(params, "indexType");
        if (cancel) {
            return ResponseEntity.ok(R.ok("取消本地序号成功"));
        } else {
            return ResponseEntity.ok(R.error("取消本地序号失败"));
        }
    }

    /**
     * 批量保存显示列表关联集团信息
     *
     * @param datalist
     *            显示列表信息
     * @return 返回信息
     */
    @RequestMapping("/saveListTabulateDept")
    public ResponseEntity<?> saveListTabulateDept(@RequestBody String datalist) {
        int code = sysTabulateDeptService.saveListTablateDeptJson(datalist);
        return getSaveResponseEntity(code);
    }

}
