package com.bonus.sharedstation.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.core.utils.StringUtils;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.security.utils.SecurityUtils;
import com.bonus.sharedstation.app.domain.MlMedicalInformation;
import com.bonus.sharedstation.app.dto.MlMedicalInformationDTO;
import com.bonus.sharedstation.app.mapper.MlMedicalInformationMapper;
import com.bonus.sharedstation.app.service.IMlMedicalInformationService;
import com.bonus.sharedstation.common.Constant.Constant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 名流预约医院信息表(MlMedicalInformation)表服务实现类
 *
 * @author 云归
 * @since 2024-12-09 17:10:03
 */
@Service("mlMedicalInformationService")
@Slf4j
public class MlMedicalInformationServiceImpl extends ServiceImpl<MlMedicalInformationMapper, MlMedicalInformation> implements IMlMedicalInformationService {
    @Resource
    private MlMedicalInformationMapper mlMedicalInformationDao;

    @Override
    public MlMedicalInformation queryById(MlMedicalInformation mlMedicalInformation) {
        return this.baseMapper.queryById(mlMedicalInformation);
    }

    @Override
    public Page<MlMedicalInformation> getListPage(MlMedicalInformationDTO dto) {
        LambdaQueryWrapper<MlMedicalInformation> queryWrapper = new LambdaQueryWrapper<>();
        Page<MlMedicalInformation> page = new Page<>(dto.getCurrentPage(), dto.getLimit());
        queryWrapper
                .eq(MlMedicalInformation::getCustId, dto.getCustId())
                .eq(MlMedicalInformation::getIsDeleted, dto.getIsDeleted())
                .orderByDesc(MlMedicalInformation::getCreatedTime);
        //处理参数问题
        return mlMedicalInformationDao.selectPage(page, queryWrapper);
    }



    /**
     * 新增数据
     *
     * @param mlMedicalInformation 实例对象
     * @return 实例对象
     */
    @Override
    public AjaxResult insert(MlMedicalInformation mlMedicalInformation) {
        if(StringUtils.isEmpty(mlMedicalInformation.getMlReturnStatus())){
            throw new ServiceException("预约名流服务异常,请联系管理员");
        }
        if (Constant.FAILED.equals(mlMedicalInformation.getMlReturnStatus())){
            throw new ServiceException("预约名流医疗服务异常,请联系管理员");
        }

        if (StringUtils.isNotEmpty(mlMedicalInformation.getCustId())){
            mlMedicalInformation.setCreatedBy(SecurityUtils.getUsername());
        }
        mlMedicalInformation.setCreatedTime(new Date());

        int insert = this.mlMedicalInformationDao.insert(mlMedicalInformation);
        if (insert>0){
            return AjaxResult.success("新增成功");
        }else{
            return AjaxResult.error("新增失败");
        }
    }

    /**
     * 修改数据
     *
     * @param mlMedicalInformation 实例对象
     * @return 实例对象
     */
    @Override
    public MlMedicalInformation update(MlMedicalInformation mlMedicalInformation) {
        if (StringUtils.isNotEmpty(mlMedicalInformation.getCustId())){
            mlMedicalInformation.setUpdatedBy(SecurityUtils.getUsername());
            mlMedicalInformation.setUpdatedTime(new Date());
        }
        this.mlMedicalInformationDao.update(mlMedicalInformation);
        return this.queryById(mlMedicalInformation);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.mlMedicalInformationDao.deleteById(id) > 0;
    }

}
