package com.fs.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fs.app.dto.DoctorDto;
import com.fs.app.service.AppDoctorService;
import com.fs.audit.dto.CreateAuditRequestDTO;
import com.fs.audit.enums.AuditTypeEnum;
import com.fs.audit.service.AuditService;
import com.fs.common.BeanCopyUtils;
import com.fs.common.exception.CustomException;
import com.fs.doctor.domain.Doctor;
import com.fs.doctor.domain.LimitCycle;
import com.fs.doctor.enums.DoctorAuditStatus;
import com.fs.doctor.mapper.DoctorMapper;
import com.fs.doctor.mapper.LimitCycleMapper;
import com.fs.doctor.service.IDoctorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 讲者Service业务层处理
 *
 * @author fs
 * @date 2025-12-30
 */
@Service
public class AppDoctorServiceImpl implements AppDoctorService {
    @Autowired
    private DoctorMapper doctorMapper;
    @Autowired
    private AuditService auditService;
    @Autowired
    private IDoctorService doctorService;
    @Autowired
    private LimitCycleMapper limitCycleMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int add(DoctorDto doctorVO, Long userId) {
        Doctor doctor = BeanCopyUtils.copy(doctorVO, Doctor.class);


        doctor.setId(userId);

        // 认证时给医生设置默认密码 ： 123456
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        doctor.setPwd(encoder.encode("123456"));
        doctor.setStatus(DoctorAuditStatus.PENDING.getCode());
        int insertCount = doctorMapper.updateDoctor(doctor);
        if (insertCount != 1 || doctor.getId() == null) {
            throw new RuntimeException("Doctor修改认证信息失败");
        }

        CreateAuditRequestDTO dto = new CreateAuditRequestDTO();
        dto.setBusinessId(doctor.getId());
        dto.setBusinessType(AuditTypeEnum.DOCTOR_AUDIT.getCode());
        dto.setTempCode(AuditTypeEnum.DOCTOR_AUDIT.getCode());
        dto.setInitiatorId(doctorVO.getCompanyUserId());
        dto.setCompanyId(doctor.getCompanyId());

        Long auditId = auditService.createAudit(dto);
        if (auditId == null) {
            throw new RuntimeException("创建审核实例失败：auditId为空");
        }

        int upd = doctorMapper.updateAuditInstanceIdById(doctor.getId(), auditId);
        if (upd != 1) {
            throw new RuntimeException("回写auditInstanceId失败");
        }

        return insertCount;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addBankCard(Doctor doctor) {
        Integer count = limitCycleMapper.selectCount(new LambdaQueryWrapper<LimitCycle>()
                .eq(LimitCycle::getLimitCycleKey, "day")
                .eq(LimitCycle::getLimitType, "updateBankCard")
                .eq(LimitCycle::getUserId, doctor.getId()));
        if (count == 3) {
            throw new CustomException("每天最多只能修改银行卡3次");
        }

        doctorService.updateDoctor(doctor);
        LimitCycle limitCycle = new LimitCycle();
        limitCycle.setUserId(doctor.getId());
        limitCycle.setLimitType("updateBankCard");
        limitCycle.setLimitCycleKey("day");
        limitCycleMapper.insert(limitCycle);
    }

}
