package com.mortals.xhx.module.appointment.service.impl;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.IdcardUtil;
import com.mortals.framework.common.Rest;
import com.mortals.xhx.module.appointment.model.AppointmentConstraintQuery;
import com.mortals.xhx.module.appointment.model.AppointmentRecordsEntity;
import com.mortals.xhx.module.person.model.PersonEntity;
import com.mortals.xhx.module.person.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.xhx.module.appointment.dao.AppointmentConstraintDao;
import com.mortals.xhx.module.appointment.model.AppointmentConstraintEntity;
import com.mortals.xhx.module.appointment.service.AppointmentConstraintService;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

/**
 * AppointmentConstraintService
 * 预约自动签到服务约束 service实现
 *
 * @author zxfei
 * @date 2023-04-09
 */
@Service("appointmentConstraintService")
public class AppointmentConstraintServiceImpl extends AbstractCRUDServiceImpl<AppointmentConstraintDao, AppointmentConstraintEntity, Long> implements AppointmentConstraintService {

    @Override
    protected void saveBefore(AppointmentConstraintEntity entity, Context context) throws AppException {
        if(StringUtils.isEmpty(entity.getName())){
            throw new AppException("姓名不能为空");
        }
        if(StringUtils.isEmpty(entity.getIdCardNo())){
            throw new AppException("身份证号码不能为空");
        }
        if(!IdcardUtil.isValidCard(entity.getIdCardNo())){
            throw new AppException("身份证号码格式不正确,请输入正确的身份证号码");
        }
        if(!Validator.isMobile(entity.getContact())){
            throw new AppException("联系方式格式不正确,请输入正确的手机号码");
        }
        entity.setOperator(context.getUser() == null ? "" : context.getUser().getRealName());
        int count = this.count(new AppointmentConstraintQuery().idCardNo(entity.getIdCardNo()), context);
        if (count > 0) {
            throw new AppException("添加人员身份证号码重复:" + entity.getIdCardNo());
        }
        super.saveBefore(entity, context);
    }

    @Override
    protected void updateBefore(AppointmentConstraintEntity entity, Context context) throws AppException {
        AppointmentConstraintEntity appointmentConstraintEntity = this.selectOne(new AppointmentConstraintQuery().idCardNo(entity.getIdCardNo()));
        if (ObjectUtils.isEmpty(appointmentConstraintEntity)) {
            entity.setId(appointmentConstraintEntity.getId());
        }
        entity.setOperator(context.getUser() == null ? "" : context.getUser().getRealName());
        super.updateBefore(entity, context);
    }

    @Override
    public Rest<String> constraintService(AppointmentConstraintEntity appointmentConstraintEntity, Context context) {
        //取消或者开启约束服务
        AppointmentConstraintEntity condition = new AppointmentConstraintEntity();
        condition.setId(appointmentConstraintEntity.getId());
        condition.setConstraint(appointmentConstraintEntity.getConstraint());
        this.getDao().update(condition);
        return Rest.ok("修改成功");
    }
}