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.CheckMethod;
import com.openhis.check.service.ICheckMethodService;
import com.openhis.web.check.appservice.ICheckMethodAppService;
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 CheckMethodAppServiceImpl implements ICheckMethodAppService {

    @Resource
    private ICheckMethodService checkMethodService;

    @Override
    public R<?> getCheckMethodList() {
        List<CheckMethod> list = checkMethodService.list();
        return R.ok(list);
    }

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

    @Override
    public R<?> addCheckMethod(CheckMethod checkMethod) {
        //1.数据校验
        if (ObjectUtil.isEmpty(checkMethod.getName())) {
            return R.fail("检查方法名称不能为空！");
        }
        if (ObjectUtil.isEmpty(checkMethod.getCode())) {
            return R.fail("检查方法代码不能为空！");
        }
        if (ObjectUtil.isEmpty(checkMethod.getCheckType())) {
            return R.fail("检查方法的检查类型不能为空！");
        }
        //2.保存
        boolean save = checkMethodService.save(checkMethod);
        return R.ok(save);
    }

    @Override
    public R<?> updateCheckMethod(CheckMethod checkMethod) {
        //1.数据校验
        if (ObjectUtil.isEmpty(checkMethod.getName())) {
            return R.fail("检查方法名称不能为空！");
        }
        if (ObjectUtil.isEmpty(checkMethod.getCode())) {
            return R.fail("检查方法代码不能为空！");
        }
        if (ObjectUtil.isEmpty(checkMethod.getCheckType())) {
            return R.fail("检查方法的检查类型不能为空！");
        }
        //2.更新
        boolean b = checkMethodService.updateById(checkMethod);
        return R.ok(b);
    }

    @Override
    public R<?> removeCheckMethod(Integer checkMethodId) {
        boolean remove = checkMethodService.removeById(checkMethodId);
        return R.ok(remove);
    }

    @Override
    public R<?> exportCheckMethod(String checkType, String name, String packageName, HttpServletResponse response) {
        LambdaQueryWrapper<CheckMethod> wrapper = new LambdaQueryWrapper<>();
        if (checkType != null && ObjectUtil.isNotEmpty(checkType)) {
            wrapper.eq(CheckMethod::getCheckType, checkType);
        }
        if (name != null && ObjectUtil.isNotEmpty(name)) {
            wrapper.like(CheckMethod::getName, name);
        }
        if (packageName != null && ObjectUtil.isNotEmpty(packageName)) {
            wrapper.eq(CheckMethod::getPackageName, packageName);
        }
        List<CheckMethod> list = checkMethodService.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("orderNum", "序号");
            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成功");
    }
}
