package com.sy.coinage.sys.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sy.coinage.core.annotation.CurrentUser;
import com.sy.coinage.core.aop.annotation.Operation;
import com.sy.coinage.core.aop.annotation.Permission;
import com.sy.coinage.core.constants.Constants;
import com.sy.coinage.core.controller.BaseController;
import com.sy.coinage.core.interceptor.Create;
import com.sy.coinage.core.interceptor.Update;
import com.sy.coinage.core.req.IdReq;
import com.sy.coinage.core.vo.CacheUserVO;
import com.sy.coinage.core.vo.OptVO;
import com.sy.coinage.sys.model.Dept;
import com.sy.coinage.sys.req.AdminDeptListReq;
import com.sy.coinage.sys.req.AdminDeptOptReq;
import com.sy.coinage.sys.req.AdminDeptReq;
import com.sy.coinage.sys.req.DeptListReq;
import com.sy.coinage.sys.req.DeptOptReq;
import com.sy.coinage.sys.req.DeptReq;
import com.sy.coinage.sys.service.IDeptService;
import com.sy.coinage.sys.vo.DeptListVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.Date;
import java.util.List;
import javax.validation.Valid;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 部门Controller
 */
@Api(value = "部门接口", tags = "016_部门")
@RestController
@RequestMapping(value = "/dept")
public class DeptController extends BaseController {

    @Autowired
    private IDeptService deptService;

    /**
     * 部门分页列表
     */
    @ApiOperation(value = "部门分页列表")
    @PostMapping("/list")
    public IPage<DeptListVO> list(@RequestBody DeptListReq req, @CurrentUser CacheUserVO user) {
        IPage<DeptListVO> data = this.deptService.pageList(req, user.getTenantId());
        return data;
    }

    /**
     * 部门详情
     */
    @ApiOperation(value = "部门详情")
    @PostMapping("/get")
    public Dept get(@RequestBody @Valid IdReq req) {
        Dept data = this.deptService.getById(req.getId());
        return data;
    }

    /**
     * 部门树形列表
     */
    @ApiOperation(value = "部门树形列表")
    @PostMapping("/treeList")
    public List<DeptListVO> treeList(@RequestBody DeptListReq req, @CurrentUser CacheUserVO user) {
        List<DeptListVO> data = this.deptService.treeList(req.getOrgId(), user.getTenantId());
        return data;
    }

    /**
     * 新增部门
     */
    @ApiOperation(value = "新增部门")
    @Operation(value = "新增部门")
    @Permission("dept:add")
    @PostMapping("/add")
    public Object add(@RequestBody @Validated({Create.class}) DeptReq req, @CurrentUser CacheUserVO user) {
        this.deptService.validName(null, req.getName(), user.getTenantId());
        Dept model = new Dept();
        BeanUtils.copyProperties(req, model);
        model.setTenantId(user.getTenantId());
        model.setCreateBy(user.getRealName());
        this.deptService.save(model);
        return "";
    }

    /**
     * 修改部门
     */
    @ApiOperation(value = "修改部门")
    @Operation(value = "修改部门")
    @Permission("dept:update")
    @PostMapping("/update")
    public Object update(@RequestBody @Validated({Update.class}) DeptReq req, @CurrentUser CacheUserVO user) {
        Dept model = this.deptService.getById(req.getId());
        // 验证是否相同租户
        this.validTenant(user, model.getTenantId());
        this.deptService.validName(req.getId(), req.getName(), model.getTenantId());
        BeanUtils.copyProperties(req, model);
        this.deptService.updateById(model);
        return "";
    }

    /**
     * 修改部门状态
     */
    @ApiOperation(value = "修改部门状态")
    @Operation(value = "修改部门状态")
    @Permission("dept:update")
    @PostMapping("/status")
    public Integer status(@RequestBody @Valid IdReq req, @CurrentUser CacheUserVO user) {
        Dept model = this.deptService.getById(req.getId());
        // 验证是否相同租户
        this.validTenant(user, model.getTenantId());
        if (null != model.getStatus() && model.getStatus().intValue() == Constants.STATUS_FORBIDDEN) {
            model.setStatus(Constants.STATUS_USABLE);
        } else {
            model.setStatus(Constants.STATUS_FORBIDDEN);
        }
        this.deptService.updateById(model);
        return model.getStatus();
    }

    /**
     * 部门选项
     */
    @ApiOperation(value = "部门选项")
    @PostMapping("/opt")
    public List<OptVO> opt(@RequestBody DeptOptReq req, @CurrentUser CacheUserVO user) {
        List<OptVO> data = this.deptService.opt(req.getOrgId(), user.getTenantId());
        return data;
    }

    /**
     * 部门树形选项
     */
    @ApiOperation(value = "部门树形选项")
    @PostMapping("/treeOpt")
    public List<OptVO> treeOpt(@RequestBody DeptOptReq req, @CurrentUser CacheUserVO user) {
        List<OptVO> data = this.deptService.treeOpt(req.getOrgId(), user.getTenantId());
        return data;
    }

    // ========================租户管理员========================

    /**
     * 租户管理员-部门分页列表
     */
    @ApiOperation(value = "租户管理员-部门分页列表")
    @PostMapping("/admin/list")
    public IPage<DeptListVO> adminList(@RequestBody AdminDeptListReq req) {
        IPage<DeptListVO> data = this.deptService.pageList(req, req.getTenantId());
        return data;
    }

    /**
     * 租户管理员-部门树形列表
     */
    @ApiOperation(value = "租户管理员-部门树形列表")
    @PostMapping("/admin/treeList")
    public List<DeptListVO> adminTreeList(@RequestBody AdminDeptListReq req) {
        List<DeptListVO> data = this.deptService.treeList(req.getOrgId(), req.getTenantId());
        return data;
    }

    /**
     * 租户管理员-部门选项
     */
    @ApiOperation(value = "租户管理员-部门选项")
    @PostMapping("/admin/opt")
    public List<OptVO> adminOpt(@RequestBody @Valid AdminDeptOptReq req) {
        List<OptVO> data = this.deptService.opt(req.getOrgId(), req.getTenantId());
        return data;
    }

    /**
     * 租户管理员-部门树形选项
     */
    @ApiOperation(value = "部门树形选项")
    @PostMapping("/admin/treeOpt")
    public List<OptVO> adminTreeOpt(@RequestBody @Valid AdminDeptOptReq req) {
        List<OptVO> data = this.deptService.treeOpt(req.getOrgId(), req.getTenantId());
        return data;
    }

    /**
     * 租户管理员-新增部门
     *
     * @param req
     * @param user
     * @return
     */
    @ApiOperation(value = "租户管理员-新增部门")
    @Operation(value = "租户管理员-新增部门")
    @PostMapping("/admin/add")
    public Object adminAdd(@RequestBody @Validated({Create.class}) AdminDeptReq req, @CurrentUser CacheUserVO user) {
        // 验证是否租户管理员
        this.validAdmin(user);
        this.deptService.validName(null, req.getName(), req.getTenantId());
        Dept model = new Dept();
        BeanUtils.copyProperties(req, model);
        model.setCreateBy(user.getRealName());
        this.deptService.save(model);
        return "";
    }

}
