package com.crm.manager.controller;

import com.crm.manager.service.UserDepartmentService;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.rely.backend.core.dto.base.BaseResultWithPagerDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.dto.user.department.UserDepartmentSearchDto;
import com.crm.rely.backend.core.entity.user.department.UserDepartmentAddEntity;
import com.crm.rely.backend.core.entity.user.department.UserDepartmentDeleteEntity;
import com.crm.rely.backend.core.entity.user.department.UserDepartmentSearchEntity;
import com.crm.rely.backend.core.entity.user.department.UserDepartmentUpdateEntity;
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;

/**
 * @program: crm-backend
 * @description:
 * @author: houn
 * @create: 2019-07-25 15:10
 */
@RestController
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private UserDepartmentService userDepartmentService;

    /**
     * 添加部门
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/add")
    public BaseResultDto add(@RequestBody @Validated UserDepartmentAddEntity entity) throws Exception {

        if (entity.getSubIndex() == null || entity.getSubIndex() < 0) {
            entity.setSubIndex(0);
        }

        userDepartmentService.add(entity);

        return BaseResultDto.success();
    }

    /**
     * 删除部门
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/delete")
    public BaseResultDto delete(@RequestBody @Validated UserDepartmentDeleteEntity entity) throws Exception {

        userDepartmentService.delete(entity);

        return BaseResultDto.success();
    }

    /**
     * 修改部门
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/update")
    public BaseResultDto update(@RequestBody @Validated UserDepartmentUpdateEntity entity) throws Exception {

        if (entity.getSubIndex() == null || entity.getSubIndex() < 0) {
            entity.setSubIndex(0);
        }

        userDepartmentService.update(entity);

        return BaseResultDto.success();
    }

    /**
     * 查询部门 page
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/search/page")
    public BaseResultDto searchPage(@RequestBody @Validated UserDepartmentSearchEntity entity) throws Exception {

        BaseResultWithPagerDto result = userDepartmentService.searchPage(entity);

        return result;
    }

    @PostMapping("/search/list")
    public BaseResultDto searchList() throws Exception {

        BaseResultDto result = userDepartmentService.searchList();

        return result;
    }

    /**
     * 单个查询
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/search/single")
    public BaseResultDto searchSingle(@RequestBody @Validated SingleLongEntity entity) throws Exception {

        UserDepartmentSearchDto userDepartmentSearchDto = userDepartmentService.searchSingle(entity.getId());

        return BaseResultDto.success(userDepartmentSearchDto);
    }
}
