package com.tofly.base.controller;


import com.tofly.base.dto.CompanyDeptDto;
import com.tofly.base.exception.BusinessException;
import com.tofly.base.service.UnitManagementService;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.common.log.annotation.ToFlyAppLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@AllArgsConstructor
@RequestMapping("/unitManagement")
@Api(tags="单位管理接口")
public class UnitManagementContriller {

    @Autowired
    private UnitManagementService unitManagementService;

    @GetMapping("/getTreeList" )
    @ApiOperation(value = "获取单位树形接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "companyName", value = "公司名称模糊查询", required = false, dataType = "String"),
            @ApiImplicitParam(name = "deptName", value = "部门名称模糊查询", required = false, dataType = "String")
    })
    public ResultRespone getSystemPage(String companyName, String deptName) {

        return ResultRespone.success(unitManagementService.getTreeList(companyName,deptName));
    }


    @ToFlyAppLog(title="新增单位/部门信息" )
    @ApiOperation(value = "新增单位/部门信息")
    @PostMapping("/saveCompanyDept")
    public ResultRespone save(@RequestBody CompanyDeptDto dto) {
        ResultRespone respone = new ResultRespone();
        try {
            respone.setResult(unitManagementService.saveCompanyDept(dto.getType(), dto.getCompany(), dto.getDepartment()));
        }catch (BusinessException businessException) {
        respone.setCode(businessException.getRetCd());
        respone.setMessage(businessException.getMsgDes());
    }
        return respone;
    }

    @ToFlyAppLog(title="修改单位/部门信息" )
    @ApiOperation(value = "修改单位/部门信息")
    @PutMapping("/updateCompanyDept")
    public ResultRespone update(@RequestBody CompanyDeptDto dto) {
        ResultRespone respone = new ResultRespone();
        try {
            respone.setResult(unitManagementService.updateCompanyDept(dto.getType(), dto.getCompany(), dto.getDepartment()));
        }catch (BusinessException businessException) {
            respone.setCode(businessException.getRetCd());
            respone.setMessage(businessException.getMsgDes());
        }
        return respone;
    }


    @ApiOperation(value = "通过ID批量删除新增单位/部门信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "修改类型,1 为单位,2为部门", required = true, dataType = "String"),
            @ApiImplicitParam(name = "ids", value = "批量删除，多个id以逗号隔开", required = true, dataType = "String")
    })
    @DeleteMapping("/deleteByIds" )
    public ResultRespone removeById(String type,String ids) {
        ResultRespone respone = new ResultRespone();
        try {
            respone.setResult(unitManagementService.deleteCompanyDept(type,ids));
        }catch (BusinessException businessException) {
            respone.setCode(businessException.getRetCd());
            respone.setMessage(businessException.getMsgDes());
        }
        return respone;
    }
}
