package com.hzs.system.sys.controller.manage;

import java.util.List;
import com.hzs.common.core.annotation.AccessPermissions;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.enums.EOperationBusiness;
import com.hzs.common.core.enums.EOperationMethod;
import com.hzs.common.core.enums.EOperationModule;
import com.hzs.common.core.enums.ENormalDisable;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.core.utils.poi.ExcelUtil;
import com.hzs.common.domain.system.sys.SysDept;
import com.hzs.system.sys.param.SysDeptQueryParam;
import com.hzs.system.sys.vo.SysDeptQueryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.hzs.common.core.constant.UserConstants;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.system.sys.service.ISysDeptService;

import javax.servlet.http.HttpServletResponse;

/**
 * 部门信息
 *
 * @author hzs
 */
@RestController
@RequestMapping("/manage/dept")
public class SysDeptController extends BaseController {

    @Autowired
    private ISysDeptService iSysDeptService;

    /**
     * 部门列表
     *
     * @param param
     * @return
     */
    @AccessPermissions("departmentRole")
    @Log(module = EOperationModule.DEPT_CONFIG, business = EOperationBusiness.DEPT_CONFIG, method = EOperationMethod.SELECT)
    @GetMapping("/list")
    public AjaxResult list(SysDeptQueryParam param) {
        DateUtils.handTimeStartAndEnd(param.getCreationTime());
        startPage();
        return AjaxResult.success(iSysDeptService.selectDeptList(param));
    }

    /**
     * 导出
     *
     * @param response
     * @param param
     */
    @Log(module = EOperationModule.DEPT_CONFIG, business = EOperationBusiness.DEPT_CONFIG, method = EOperationMethod.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysDeptQueryParam param) {
        DateUtils.handTimeStartAndEnd(param.getCreationTime());
        List<SysDeptQueryVO> list = iSysDeptService.selectDeptList(param);
        ExcelUtil<SysDeptQueryVO> util = new ExcelUtil<>(SysDeptQueryVO.class);
        util.exportExcel(response, list, "部门导出");
    }

    /**
     * 部门详情
     */
    @GetMapping(value = "/{deptId}")
    public AjaxResult getInfo(@PathVariable Long deptId) {
        return AjaxResult.success(iSysDeptService.selectDeptById(deptId));
    }

    /**
     * 新增部门
     */
    @Log(module = EOperationModule.DEPT_CONFIG, business = EOperationBusiness.DEPT_CONFIG, method = EOperationMethod.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysDept dept) {
        if (UserConstants.NOT_UNIQUE.equals(iSysDeptService.checkDeptNameUnique(dept))) {
            return AjaxResult.error("新增部门'" + dept.getDeptName() + "'失败，部门名称已存在");
        }
        dept.setPkCreator(SecurityUtils.getUserId());
        return toAjax(iSysDeptService.insertDept(dept));
    }

    /**
     * 修改部门
     */
    @Log(module = EOperationModule.DEPT_CONFIG, business = EOperationBusiness.DEPT_CONFIG, method = EOperationMethod.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysDept dept) {
        if (null == dept.getDeptId()) {
            return AjaxResult.error("缺少参数");
        }
        if (UserConstants.NOT_UNIQUE.equals(iSysDeptService.checkDeptNameUnique(dept))) {
            return AjaxResult.error("修改部门'" + dept.getDeptName() + "'失败，部门名称已存在");
        }
        dept.setPkModified(SecurityUtils.getUserId());
        return toAjax(iSysDeptService.updateDept(dept));
    }

    /**
     * 状态修改
     *
     * @param dept
     * @return
     */
    @Log(module = EOperationModule.DEPT_CONFIG, business = EOperationBusiness.DEPT_CONFIG, method = EOperationMethod.UPDATE_STATUS)
    @PutMapping("/change-status")
    public AjaxResult changeStatus(@RequestBody SysDept dept) {
        if (null == dept.getDeptId()) {
            return AjaxResult.error("缺少参数");
        }
        if (ENormalDisable.STOP.getValue() == dept.getStatus() && iSysDeptService.checkDeptExistUser(dept.getDeptId())) {
            return AjaxResult.error("部门存在用户,不允许停用");
        }
        dept.setPkModified(SecurityUtils.getUserId());
        return toAjax(iSysDeptService.updateDept(dept));
    }

    /**
     * 删除部门
     */
    @Log(module = EOperationModule.DEPT_CONFIG, business = EOperationBusiness.DEPT_CONFIG, method = EOperationMethod.DELETE)
    @DeleteMapping("/{deptId}")
    public AjaxResult remove(@PathVariable Long deptId) {
        if (iSysDeptService.checkDeptExistUser(deptId)) {
            return AjaxResult.error("部门存在用户,不允许删除");
        }
        return toAjax(iSysDeptService.deleteDeptById(deptId, SecurityUtils.getUserId()));
    }


    /**
     * 全部部门
     *
     * @return
     */
    @GetMapping("/all")
    public AjaxResult all() {
        return AjaxResult.success(iSysDeptService.selectDeptList(new SysDeptQueryParam()));
    }

}
