package com.fs.app.service.impl;

import com.fs.app.dto.ConfirmTicketDto;
import com.fs.app.dto.DoctorDto;
import com.fs.app.service.AppDoctorService;
import com.fs.app.service.AppServiceTicketService;
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.doctor.domain.Doctor;
import com.fs.doctor.domain.DoctorBalanceLog;
import com.fs.doctor.mapper.DoctorMapper;
import com.fs.doctor.service.IDoctorBalanceLogService;
import com.fs.provider.domain.ServiceOrder;
import com.fs.provider.domain.ServiceOrderTaskRel;
import com.fs.provider.enums.BalanceBusinessType;
import com.fs.provider.enums.BalanceChangeTypeEnum;
import com.fs.provider.enums.DoctorConfirmEnum;
import com.fs.provider.enums.SettlementStatusEnum;
import com.fs.provider.mapper.ServiceOrderMapper;
import com.fs.provider.mapper.ServiceOrderTaskRelMapper;
import com.fs.task.domain.TaskInfo;
import com.fs.task.enums.TaskFinishStatusEnum;
import com.fs.task.mapper.TaskInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 讲者Service业务层处理
 *
 * @author fs
 * @date 2025-12-30
 */
@Service
public class AppServiceTicketServiceImpl implements AppServiceTicketService {
    @Autowired
    private IDoctorBalanceLogService iDoctorBalanceLogService;
    @Autowired
    private DoctorMapper doctorMapper;
    @Autowired
    private TaskInfoMapper taskInfoMapper;
    @Autowired
    private ServiceOrderMapper serviceOrderMapper;
    @Autowired
    private ServiceOrderTaskRelMapper serviceOrderTaskRelMapper;

    @Override
    @Transactional
    public void confirmServiceTicket(ConfirmTicketDto confirmTicketDto) {
        Doctor doctor = doctorMapper.selectDoctorById(Long.parseLong(confirmTicketDto.getUserId()));
        // 添加积分流转记录
        DoctorBalanceLog doctorBalanceLog = new DoctorBalanceLog();
        doctorBalanceLog.setBusinessId(confirmTicketDto.getServiceOrderId());
        doctorBalanceLog.setBusinessType(BalanceBusinessType.SERVICE_TICKET.toString());
        doctorBalanceLog.setDoctorId(Long.parseLong(confirmTicketDto.getUserId()));
        doctorBalanceLog.setDoctorName(doctor.getDoctorName());
        doctorBalanceLog.setChangeType(BalanceChangeTypeEnum.INCREASE.getCode());
        doctorBalanceLog.setChangeMoney(confirmTicketDto.getTotalAmount());
        doctorBalanceLog.setRelatedTaskId(confirmTicketDto.getServiceOrderId());
        iDoctorBalanceLogService.insertDoctorBalanceLog(doctorBalanceLog);

        // 修改医生账户余额
        doctorMapper.addPoints(Long.parseLong(confirmTicketDto.getUserId()),confirmTicketDto.getTotalAmount());

        // 根据服务单查询所有任务项
        ServiceOrderTaskRel serviceOrderTaskRel = new ServiceOrderTaskRel();
        serviceOrderTaskRel.setServiceOrderId(confirmTicketDto.getServiceOrderId());
        List<ServiceOrderTaskRel> serviceOrderTaskRels = serviceOrderTaskRelMapper.selectServiceOrderTaskRelList(serviceOrderTaskRel);
        List<Long> taskIds = serviceOrderTaskRels.stream()
                .map(ServiceOrderTaskRel::getTaskId)
                .collect(Collectors.toList());

        List<TaskInfo> taskInfos = taskInfoMapper.selectTaskInfoByIds(taskIds);
        taskInfos.forEach(taskInfo -> {
            // 修改任务单为已完成
            taskInfo.setFinishStatus(Long.parseLong(TaskFinishStatusEnum.FINISHED.getCode().toString()));
            taskInfoMapper.updateTaskInfo(taskInfo);
        });

        // 改变服务单确认状态
        ServiceOrder serviceOrder = new ServiceOrder();
        serviceOrder.setId(confirmTicketDto.getServiceOrderId());
        serviceOrder.setDoctorConfirm(DoctorConfirmEnum.CONFIRMED.getCode().longValue());
        serviceOrder.setSettlementStatus(SettlementStatusEnum.SETTLED.getCode());
        serviceOrderMapper.updateServiceOrder(serviceOrder);






    }
}
