package com.mx.cneeds.server.user.web;

import com.mx.cneeds.common.dto.DepartmentDto;
import com.mx.cneeds.common.pager.Constant;
import com.mx.cneeds.common.pager.PageUtils;
import com.mx.cneeds.common.result.R;
import com.mx.cneeds.common.validator.ValidatorUtils;
import com.mx.cneeds.server.entity.SysDeptEntity;
import com.mx.cneeds.server.user.service.SysDeptService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

//import org.apache.shiro.authz.annotation.RequiresPermissions;


/**
 * 部门表
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2020-01-09 15:09:01
 */
@RestController
@RequestMapping("sys/dept")
public class SysDeptController {
    @Autowired
    private SysDeptService sysDeptService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public PageUtils list(@RequestParam Map<String, Object> params){
//        PageUtils page = sysDeptService.queryPage(params);
        List<SysDeptEntity> sysDeptEntityList = sysDeptService.queryListByPage(params);

        long count = sysDeptService.queryDeptCount(params);
        long limit = (long) params.get(Constant.LIMIT);
        long Curpage = (long) params.get(Constant.PAGE);
        PageUtils page = new PageUtils(sysDeptEntityList,
                (int) count,
                (int)limit,
                (int)Curpage);
        return page;
    }

    @RequestMapping("/listAll")
    public List<DepartmentDto> listAll(@RequestParam Map<String, Object> params){
        List<SysDeptEntity> sysDeptEntityList = sysDeptService.queryList(params);
        List<DepartmentDto> departmentDtoList = new ArrayList<>();
        sysDeptEntityList.forEach(sysDeptEntity -> {
            DepartmentDto departmentDto = new DepartmentDto();
            BeanUtils.copyProperties(sysDeptEntity,departmentDto);
            departmentDtoList.add(departmentDto);
        });
        return departmentDtoList;
    }

    /**
     * 信息
     */
    @RequestMapping("/info")
//    @RequiresPermissions("sys:sysdept:info")
    public DepartmentDto info(@RequestBody Long deptId){
        SysDeptEntity sysDept = sysDeptService.getById(deptId);
        DepartmentDto dto = new DepartmentDto();
        if (sysDept == null){
            return null;
        }
        BeanUtils.copyProperties(sysDept,dto);
        return dto;
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("sys:sysdept:save")
    public R save(@RequestBody DepartmentDto departmentDto){
        SysDeptEntity sysDept = new SysDeptEntity();
        BeanUtils.copyProperties(departmentDto,sysDept);
        sysDeptService.save(sysDept);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("sys:sysdept:update")
    public R update(@RequestBody SysDeptEntity sysDept){
        ValidatorUtils.validateEntity(sysDept);
        sysDeptService.updateById(sysDept);
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("sys:sysdept:delete")
    public R delete(@RequestBody List<Long> ids){
        sysDeptService.removeByIds(ids);
        return R.ok();
    }

}
