package com.xinelu.manage.controller.department;

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.department.Department;
import com.xinelu.manage.dto.department.DepartmentDTO;
import com.xinelu.manage.service.department.IDepartmentService;
import com.xinelu.manage.service.questioninfo.IQuestionInfoService;
import com.xinelu.manage.vo.department.DepartmentBaseVo;
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/department")
public class DepartmentController extends BaseController {
    @Resource
    private IDepartmentService departmentService;
	@Resource
	private IQuestionInfoService questionInfoService;

    /**
     * 查询科室信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:department:list')")
    @GetMapping("/list")
    public TableDataInfo list(Department department) {
        startPage();
        List<Department> list = departmentService.selectDepartmentList(department);
        return getDataTable(list);
    }

    /**
     * 查询科室信息列表 不分页
     */
    @GetMapping("/getDepartmentList")
    public AjaxResult getDepartmentList(Department department) {
        return AjaxResult.success(departmentService.selectDepartmentList(department));
    }

    /**
     * 查询科室信息列表 问卷使用
     */
    @GetMapping("/selectUserDepartment")
    public AjaxResult selectUserDepartment() {
        return departmentService.selectUserDepartment();
    }

    /**
     * 查询科室或病区信息列表 不分页
     */
    @GetMapping("/selectDepartmentList")
    public AjaxResult selectDepartmentByAgencyIdList(Department department) {
        return departmentService.selectDepartmentByAgencyIdList(department);
    }

    /**
     * 导出科室信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:department:export')")
    @Log(title = "科室信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Department department) {
        List<Department> list = departmentService.selectDepartmentList(department);
        ExcelUtil<Department> util = new ExcelUtil<Department>(Department.class);
        util.exportExcel(response, list, "科室信息数据");
    }

    /**
     * 获取科室信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:department:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(departmentService.selectDepartmentById(id));
    }

    /**
     * 新增科室信息
     */
    @PreAuthorize("@ss.hasPermi('system:department:add')")
    @Log(title = "科室信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Department department) {
        return departmentService.insertDepartment(department);
    }

    /**
     * 修改科室信息
     */
    @PreAuthorize("@ss.hasPermi('system:department:edit')")
    @Log(title = "科室信息", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody Department department) {
        return departmentService.updateDepartment(department);
    }

    /**
     * 删除科室信息
     */
    @PreAuthorize("@ss.hasPermi('system:department:remove')")
    @Log(title = "科室信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return departmentService.deleteDepartmentByIds(ids);
    }

    /**
     * 查询科室信息列表及包含话术数量
     */
    @GetMapping("/listScriptNum")
    public AjaxResult listScriptNum(DepartmentDTO departmentDto) {
	    DepartmentBaseVo departmentBaseVo = departmentService.selectDepartmentListScriptNum(departmentDto);
        return AjaxResult.success(departmentBaseVo);
    }

    /**
     * 查询科室信息列表及包含手术数量
     */
    @GetMapping("/listOperationNum")
    public AjaxResult listOperationNum(DepartmentDTO departmentDto) {
	    DepartmentBaseVo departmentBaseVo = departmentService.selectDepartmentListOperationNum(departmentDto);
        return AjaxResult.success(departmentBaseVo);
    }

    /**
     * 查询科室信息列表及包含微信库数量
     */
    @GetMapping("/listWechatTemplateNum")
    public AjaxResult listWechatTemplateNum(DepartmentDTO departmentDto) {
	    DepartmentBaseVo departmentBaseVo = departmentService.selectDepartmentListWechatTemplateNum(departmentDto);
        return AjaxResult.success(departmentBaseVo);
    }

    /**
     * 查询科室信息列表及包含短信库数量
     */
    @GetMapping("/listMessageNum")
    public AjaxResult listMessageNum(DepartmentDTO departmentDto) {
	    DepartmentBaseVo departmentBaseVo = departmentService.selectDepartmentListMessageNum(departmentDto);
        return AjaxResult.success(departmentBaseVo);
    }

    /**
     * 导入科室信息
     *
     * @param file 模板文件
     * @return 导入结果
     * @throws Exception 异常信息
     */
    @PreAuthorize("@ss.hasPermi('system:agency:importStationInfo')")
    @Log(title = "导入科室信息", businessType = BusinessType.IMPORT)
    @PostMapping("/insertDepartmentList")
    public AjaxResult insertDepartmentList(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<Department> util = new ExcelUtil<>(Department.class);
        List<Department> list = util.importExcel(file.getInputStream());
        return departmentService.insertDepartmentList(list);
    }

    /**
     * 查询科室信息列表及包含服务包数量
     */
    @GetMapping("/listServicePackageNum")
    public AjaxResult listServicePackageNum(DepartmentDTO departmentDto) {
	    DepartmentBaseVo departmentBaseVo = departmentService.selectListServicePackageNum(departmentDto);
        return AjaxResult.success(departmentBaseVo);
    }
}