package com.xinyilu.web.controller.organize;

import com.xinyilu.common.annotation.Log;
import com.xinyilu.common.core.controller.BaseController;
import com.xinyilu.common.core.domain.AjaxResult;
import com.xinyilu.common.custominterface.Insert;
import com.xinyilu.common.custominterface.Update;
import com.xinyilu.common.enums.BusinessType;
import com.xinyilu.common.utils.poi.ExcelUtil;
import com.xinyilu.system.domain.dto.OrganizeInfoDTO;
import com.xinyilu.system.domain.organize.OrganizeInfo;
import com.xinyilu.system.domain.vo.OrganizeInfoVO;
import com.xinyilu.system.service.organize.IOrganizeInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 组织Controller
 *
 * @author jihan
 * @date 2022-07-28
 */
@RestController
@RequestMapping("/system/info")
public class OrganizeInfoController extends BaseController {
    @Autowired
    private IOrganizeInfoService organizeInfoService;

    /**
     * 查询组织列表
     */
//    @PreAuthorize("@ss.hasPermi('system:info:list')")
    @GetMapping("/list")
    public AjaxResult list(OrganizeInfoVO organizeInfo) {
        List<OrganizeInfoDTO> list = organizeInfoService.selectOrganizeInfoList(organizeInfo);
        return AjaxResult.success(list);
    }

    /**
     * 导出组织列表
     */
    @PreAuthorize("@ss.hasPermi('system:info:export')")
    @Log(title = "组织", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OrganizeInfoVO organizeInfo) {
        List<OrganizeInfoDTO> list = organizeInfoService.selectOrganizeInfoList(organizeInfo);
        ExcelUtil<OrganizeInfoDTO> util = new ExcelUtil<OrganizeInfoDTO>(OrganizeInfoDTO.class);
        util.exportExcel(response, list, "组织数据");
    }

    /**
     * 获取组织详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:info:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(organizeInfoService.selectOrganizeInfoById(id));
    }

    /**
     * 新增组织
     */
    @PreAuthorize("@ss.hasPermi('system:info:add')")
    @Log(title = "新增组织", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@Validated(Insert.class) @RequestBody OrganizeInfo organizeInfo) {
        //创建人
        organizeInfo.setCreateBy(getUsername());
        return organizeInfoService.insertOrganizeInfo(organizeInfo);
    }

    /**
     * 修改组织
     */
    @PreAuthorize("@ss.hasPermi('system:info:edit')")
    @Log(title = "修改组织", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@Validated(Update.class) @RequestBody OrganizeInfo organizeInfo) {
        //修改人
        organizeInfo.setUpdateBy(getUsername());
        return organizeInfoService.updateOrganizeInfo(organizeInfo);
    }

    /**
     * 删除组织
     */
    @PreAuthorize("@ss.hasPermi('system:info:remove')")
    @Log(title = "组织", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(organizeInfoService.deleteOrganizeInfoByIds(ids));
    }
}
