package com.yx.exam.modules.depart.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yx.exam.core.api.ApiRest;
import com.yx.exam.core.api.controller.BaseController;
import com.yx.exam.core.api.dto.BaseIdReqDTO;
import com.yx.exam.core.api.dto.BaseIdsReqDTO;
import com.yx.exam.core.api.dto.PagingReqDTO;
import com.yx.exam.core.utils.BeanMapper;
import com.yx.exam.modules.depart.dto.SysDepartDTO;
import com.yx.exam.modules.depart.dto.request.DepartSortReqDTO;
import com.yx.exam.modules.depart.dto.response.SysDepartTreeDTO;
import com.yx.exam.modules.depart.entity.SysDepart;
import com.yx.exam.modules.depart.service.SysDepartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
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;

import javax.annotation.Resource;
import java.util.List;

/**
* <p>
* 部门信息控制器
* </p>
*
* @author chd
* @since 2020-09-02 17:25
*/
@Api(tags={"部门信息"})
@RestController
@RequestMapping("/exam/api/sys/depart")
public class SysDepartController extends BaseController {

    @Resource
    private SysDepartService baseService;

    /**
    * 添加或修改
    * @param reqDTO
    * @return
    */
    @ApiOperation(value = "添加或修改")
    @PostMapping("save")
    public ApiRest save(@RequestBody SysDepartDTO reqDTO) {
        baseService.save(reqDTO);
        return super.success();
    }

    /**
    * 批量删除
    * @param reqDTO
    * @return
    */
    @ApiOperation(value = "批量删除")
    @PostMapping("delete")
    public ApiRest edit(@RequestBody BaseIdsReqDTO reqDTO) {
        //根据ID删除
        baseService.removeByIds(reqDTO.getIds());
        return super.success();
    }

    /**
    * 查找详情
    * @param reqDTO
    * @return
    */
    @ApiOperation(value = "查找详情")
    @PostMapping("detail")
    public ApiRest<SysDepartDTO> find(@RequestBody BaseIdReqDTO reqDTO) {
        SysDepart entity = baseService.getById(reqDTO.getId());
        SysDepartDTO dto = new SysDepartDTO();
        BeanUtils.copyProperties(entity, dto);
        return super.success(dto);
    }

    /**
    * 分页查找
    * @param reqDTO
    * @return
    */
    @ApiOperation(value = "分页查找")
    @PostMapping("paging")
    public ApiRest<IPage<SysDepartTreeDTO>> paging(@RequestBody PagingReqDTO<SysDepartDTO> reqDTO) {
        //分页查询并转换
        IPage<SysDepartTreeDTO> page = baseService.paging(reqDTO);
        return super.success(page);
    }

    /**
     * 查找列表，每次最多返回200条数据
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "查找列表")
    @PostMapping("list")
    public ApiRest<List<SysDepartDTO>> list(@RequestBody SysDepartDTO reqDTO) {

        //分页查询并转换
        QueryWrapper<SysDepart> wrapper = new QueryWrapper<>();

        //转换并返回
        List<SysDepart> list = baseService.list(wrapper);

        //转换数据
        List<SysDepartDTO> dtoList = BeanMapper.mapList(list, SysDepartDTO.class);

        return super.success(dtoList);
    }


    /**
     * 树列表
     * @return
     */
    @ApiOperation(value = "树列表")
    @PostMapping("tree")
    public ApiRest<List<SysDepartTreeDTO>> tree() {
        List<SysDepartTreeDTO> dtoList = baseService.findTree();
        return super.success(dtoList);
    }


    /**
     * 分类排序
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "分类排序")
    @PostMapping("sort")
    public ApiRest sort(@RequestBody DepartSortReqDTO reqDTO) {
        baseService.sort(reqDTO.getId(), reqDTO.getSort());
        return super.success();
    }
}
