package easydo.technology.system.rest;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import easydo.technology.system.domain.SysDept;
import easydo.technology.system.dto.SysDeptDto;
import easydo.technology.system.mapper.SysDeptMapper;
import easydo.technology.system.service.ISysDeptService;
import easydo.technology.utils.StringUtil;
import org.springframework.http.HttpStatus;
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 javax.annotation.Resource;
import java.util.List;


/**
 * @author Zheng Jie
 * @date 2019-03-25
 */
@RestController
@RequestMapping("/dept")
public class DeptController {
    @Resource
    SysDeptMapper sysDeptMapper;
    @Resource
    ISysDeptService sysDeptService;

    @RequestMapping("/getList")
    public Object getList(@RequestBody SysDeptDto dto) {
        QueryWrapper<SysDept> wrapper = new QueryWrapper<>();
        if (StringUtil.isNotEmpty(dto.getPid())) {
            if (dto.getPid() == 0) {
                wrapper.isNull("pid");
            } else {
                wrapper.eq("pid", dto.getPid());
            }
        }
        if(StringUtil.isNotEmpty(dto.getName())){
            wrapper.like("name", dto.getName());
        }
        if (StringUtil.isNotEmpty(dto.getEnabled())) {
            wrapper.eq("enabled", dto.getEnabled());
        }
        if (StringUtil.isNotEmpty(dto.getBeginCreateTime())) {
            wrapper.gt("create_time", dto.getBeginCreateTime());
        }
        if (StringUtil.isNotEmpty(dto.getEndCreateTime())) {
            wrapper.lt("create_time", dto.getEndCreateTime());
        }
        List<SysDept> deptList = sysDeptMapper.selectList(wrapper);
        return new ResponseEntity<>(deptList, HttpStatus.OK);
    }

    @RequestMapping("/save")
    public Object save(@RequestBody SysDept dept) {
        sysDeptService.saveDept(dept);
        return new ResponseEntity<>(dept, HttpStatus.OK);
    }

    @RequestMapping("/update")
    public Object update(@RequestBody SysDept dept) throws Exception {
        sysDeptService.updateDept(dept);
        return new ResponseEntity<>(dept, HttpStatus.OK);
    }

    @RequestMapping("/remove")
    public Object remove(@RequestBody SysDeptDto dept) throws Exception {
        sysDeptService.verification(dept.getIds());
        sysDeptService.removeByIds(dept.getIds());
        return new ResponseEntity<>(dept, HttpStatus.OK);
    }
}