package com.openhis.web.check.appservice.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.core.common.core.domain.R;
import com.openhis.check.domain.CheckPart;
import com.openhis.check.service.ICheckPartService;
import com.openhis.web.check.appservice.ICheckPartAppService;
import com.openhis.web.reportmanage.utils.ExcelFillerUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@Service
@Slf4j
public class CheckPartAppServiceImpl implements ICheckPartAppService {
    @Resource
    private ICheckPartService checkPartService;
    @Override
    public R<?> getCheckPartList() {
        List<CheckPart> list = checkPartService.list();
        return R.ok(list);
    }

    @Override
    public R<?> searchCheckPartList(Integer pageNo, Integer pageSize, String checkType, String name, String packageName) {
        LambdaQueryWrapper<CheckPart> wrapper = new LambdaQueryWrapper<>();
        if (checkType != null && ObjectUtil.isNotEmpty(checkType)) {
            wrapper.eq(CheckPart::getCheckType, checkType);
        }
        if (name != null && ObjectUtil.isNotEmpty(name)) {
            wrapper.like(CheckPart::getName, name);
        }
        if (packageName != null && ObjectUtil.isNotEmpty(packageName)) {
            wrapper.eq(CheckPart::getPackageName, packageName);
        }
        List<CheckPart> list = checkPartService.list(wrapper);
        return R.ok(list);
    }

    @Override
    public R<?> addCheckPart(CheckPart checkPart) {
        //数据检验

        //保存
        boolean save = checkPartService.save(checkPart);
        return R.ok(save);
    }

    @Override
    public R<?> removeCheckPart(Integer checkPartId) {
        boolean remove = checkPartService.removeById(checkPartId);
        return R.ok(remove);
    }

    @Override
    public R<?> updateCheckPart(CheckPart checkPart) {
        boolean b = checkPartService.updateById(checkPart);
        return R.ok(b);
    }

    @Override
    public R<?> exportCheckPart(String checkType, String name, String packageName, HttpServletResponse response) {
        LambdaQueryWrapper<CheckPart> wrapper = new LambdaQueryWrapper<>();
        if (checkType != null && ObjectUtil.isNotEmpty(checkType)) {
            wrapper.eq(CheckPart::getCheckType, checkType);
        }
        if (name != null && ObjectUtil.isNotEmpty(name)) {
            wrapper.like(CheckPart::getName, name);
        }
        if (packageName != null && ObjectUtil.isNotEmpty(packageName)) {
            wrapper.eq(CheckPart::getPackageName, packageName);
        }
        List<CheckPart> list = checkPartService.list(wrapper);

        if (list.isEmpty()) {
            return R.fail("导出Excel失败,无数据。");
        }

        try {
            // 准备表头（key对应实体的字段名）
            Map<String, String> headers = new LinkedHashMap<>();
            headers.put("checkType", "检查类型");
            headers.put("code", "部位代码");
            headers.put("name", "部位名称");
            headers.put("packageName", "套餐名称");
            headers.put("exposureNum", "曝光次数");
            headers.put("price", "金额");
            headers.put("number", "序号");
            headers.put("serviceScope", "服务范围");
            headers.put("subType", "下级医技类型");
            headers.put("remark", "备注");

            // 文件名，只传文字部分
            String excelName = "检查部位列表";
            // 导出到Excel
            ExcelFillerUtil.makeExcelFile(response, list, headers, excelName, null);
        } catch (IOException | IllegalAccessException e) {
            e.printStackTrace();
            return R.fail("导出Excel失败：" + e.getMessage());
        }
        return R.ok(null, "导出Excel成功");
    }
}
