package com.xinelu.manage.controller.agency;

import com.xinelu.common.annotation.Log;
import com.xinelu.common.constant.Constants;
import com.xinelu.common.core.controller.BaseController;
import com.xinelu.common.core.domain.AjaxResult;
import com.xinelu.common.core.page.TableDataInfo;
import com.xinelu.common.enums.BusinessType;
import com.xinelu.common.utils.poi.ExcelUtil;
import com.xinelu.manage.domain.agency.Agency;
import com.xinelu.manage.dto.agency.AgencyDTO;
import com.xinelu.manage.dto.agency.HospitalDTO;
import com.xinelu.manage.service.agency.IAgencyService;
import java.util.List;
import java.util.Objects;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 机构信息Controller
 *
 * @author xinelu
 * @date 2024-02-26
 */
@RestController
@RequestMapping("/system/agency")
public class AgencyController extends BaseController {
    @Resource
    private IAgencyService agencyService;

    /**
     * 查询机构信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:agency:list')")
    @GetMapping("/list")
    public TableDataInfo list(Agency agency) {
        startPage();
        List<Agency> list = agencyService.selectAgencyList(agency);
        return getDataTable(list);
    }

    /**
     * 查询机构信息列表 - 不分页
     */
    @GetMapping("/selectAgencyList")
    public AjaxResult selectAgencyByIdList(Agency agency) {
        return AjaxResult.success(agencyService.selectAgencyByIdList(agency));
    }

    /**
     * 查询院区机构信息列表
     */
    @GetMapping("/subordinateAgencyList")
    public AjaxResult subordinateAgencyList(Agency agency) {
        if (Objects.isNull(agency) || Objects.isNull(agency.getParentId())) {
            return AjaxResult.success();
        }
        return AjaxResult.success(agencyService.selectAgencyByIdList(agency));
    }

    /**
     * 导出机构信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:agency:export')")
    @Log(title = "机构信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Agency agency) {
        List<Agency> list = agencyService.selectAgencyList(agency);
        ExcelUtil<Agency> util = new ExcelUtil<Agency>(Agency.class);
        util.exportExcel(response, list, "机构信息数据");
    }

    /**
     * 获取机构信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:agency:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(agencyService.selectAgencyById(id));
    }

    /**
     * 新增机构信息
     */
    @PreAuthorize("@ss.hasPermi('system:agency:add')")
    @Log(title = "机构信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Agency agency) {
        return agencyService.insertAgency(agency);
    }

    /**
     * 修改机构信息
     */
    @PreAuthorize("@ss.hasPermi('system:agency:edit')")
    @Log(title = "机构信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Agency agency) {
        return agencyService.updateAgency(agency);
    }

    /**
     * 删除机构信息
     */
    @PreAuthorize("@ss.hasPermi('system:agency:remove')")
    @Log(title = "机构信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return agencyService.deleteAgencyByIds(ids);
    }

    /**
     * 查询机构信息列表 - 树图
     */
    @GetMapping("/agencyList")
    public AjaxResult agencyList() {
        return AjaxResult.success(agencyService.agencyList());
    }

    /**
     * 导入机构信息
     *
     * @param file 模板文件
     * @return 导入结果
     * @throws Exception 异常信息
     */
    @PreAuthorize("@ss.hasPermi('system:agency:importStationInfo')")
    @Log(title = "导入机构信息", businessType = BusinessType.IMPORT)
    @PostMapping("/insertAgencyImportList")
    public AjaxResult insertAgencyImportList(MultipartFile file) throws Exception {
        //判断excel里面是否有数据/文件格式
        if (Objects.isNull(file) || StringUtils.isBlank(file.getOriginalFilename())) {
            return AjaxResult.error("请选择需要导入的文件！");
        }
        // 获取文件名
        String orgName = file.getOriginalFilename();
        if (!orgName.endsWith(Constants.XLSX) && !orgName.endsWith(Constants.XLS)) {
            return AjaxResult.error("导入文件格式不正确，请导入xlsx或xls格式的文件！");
        }
        ExcelUtil<AgencyDTO> util = new ExcelUtil<>(AgencyDTO.class);
        List<AgencyDTO> list = util.importExcel(file.getInputStream());
        return agencyService.insertAgencyImportList(list);
    }

    /**
     * 医院园区科室病区联动查询
     */
    @GetMapping("/getAgencyList")
    public AjaxResult getAgencyList(HospitalDTO hospitalDTO) {
        return agencyService.getAgencyList(hospitalDTO);
    }
}