package io.modules.item.service.impl;

import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import cn.hutool.core.util.StrUtil;
import io.common.exception.RenException;
import io.common.service.impl.CrudServiceImpl;
import io.common.validator.AssertUtils;
import io.modules.item.dao.DoctorsDao;
import io.modules.item.dto.DoctorsDTO;
import io.modules.item.entity.DoctorsEntity;
import io.modules.item.service.DoctorsService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

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

/**
 * 医生表
 *
 * @author Mark #
 * @since 1.0.0 2025-02-18
 */
@Service
public class DoctorsServiceImpl extends CrudServiceImpl<DoctorsDao, DoctorsEntity, DoctorsDTO> implements DoctorsService {

    @Value("${upload.url}")
    private String uploadUrl;

    @Override
    public QueryWrapper<DoctorsEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");
        String userName = (String)params.get("userName");
        String audit = (String)params.get("audit");
        String specialty = (String)params.get("departmentId");
        String intro = (String)params.get("intro");
        String position = (String)params.get("position");
        QueryWrapper<DoctorsEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(id), "id", id);
        wrapper.like(StrUtil.isNotBlank(intro), "intro", intro);
        wrapper.eq(StrUtil.isNotBlank(audit), "audit", audit);
        wrapper.eq(StrUtil.isNotBlank(position), "position", position);
        wrapper.eq(StrUtil.isNotBlank(specialty), "department_id", specialty);
        wrapper.eq(StrUtil.isNotBlank(userName), "user_name", userName);
        wrapper.orderByDesc("create_time");
        return wrapper;
    }


    @Override
    public DoctorsEntity login(DoctorsDTO dto) {
        LambdaQueryWrapper<DoctorsEntity> lwq = new LambdaQueryWrapper<>();

//        账号
        lwq.eq(DoctorsEntity::getNickName,dto.getNickName());
        if (baseDao.selectOne(lwq) == null){
            throw new RenException("用户名不存在~");
        }
        lwq.eq(DoctorsEntity::getPassword,DigestUtil.sha256Hex(dto.getPassword()));
        if (baseDao.selectOne(lwq) == null){
            throw new RenException("密码输入错误~");
        }
        DoctorsEntity entity = baseDao.selectOne(lwq);
        entity.setImg(uploadUrl + entity.getImg());
        return entity;
    }
}