package thyyxxk.webserver.service.examinations;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.constants.Capacity;
import thyyxxk.webserver.dao.his.examinations.InspectionsDao;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.examinations.bookablemanage.ExamItem;
import thyyxxk.webserver.entity.examinations.bookablemanage.SearchParam;
import thyyxxk.webserver.utils.ResultVoUtil;
import thyyxxk.webserver.utils.StringUtil;
import thyyxxk.webserver.utils.TokenUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: DingJie
 * @create: 2021-04-25 11:33:49
 **/
@Slf4j
@Service
public class BookableManageService {
    private final InspectionsDao dao;

    @Autowired
    public BookableManageService(InspectionsDao dao) {
        this.dao = dao;
    }

    public ResultVo<Map<String, Object>> searchItems(SearchParam param) {
        IPage<ExamItem> iPage = new Page<>(param.getCurrentPage(), param.getPageSize());
        if (StringUtil.isBlank(param.getSearchContent())) {
            iPage = dao.searchItems(iPage, param.getTableName());
        } else {
            String content = "%" + param.getSearchContent().trim().toUpperCase() + "%";
            iPage = dao.searchItemsByCondition(iPage, param.getTableName(), param.getSearchMethod(), content);
        }
        Map<String, Object> map = new HashMap<>(Capacity.TWO);
        map.put("list", iPage.getRecords());
        map.put("totalSize", iPage.getTotal());
        return ResultVoUtil.success(map);
    }

    public ResultVo<Integer> updateWxBookableFlag(ExamItem param) {
        if (StringUtil.isBlank(param.getBookTip())) {
            return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER, "预约提示不能为空。");
        }
        dao.updateWxBookableFlag(param);
        log.info("操作员：{}，编辑检验检查预约信息：{}", TokenUtil.getInstance().getTokenUserId(), param);
        return ResultVoUtil.success();
    }
}
