package com.fs.his.service.impl;

import com.fs.common.exception.CustomException;
import com.fs.common.utils.DateUtils;
import com.fs.his.domain.FsDoctor;
import com.fs.his.domain.FsDoctorBill;
import com.fs.his.domain.FsDoctorExtract;
import com.fs.his.mapper.FsDoctorBillMapper;
import com.fs.his.mapper.FsDoctorExtractMapper;
import com.fs.his.mapper.FsDoctorMapper;
import com.fs.his.param.FsDoctorExtractListSParam;
import com.fs.his.service.IFsDoctorExtractService;
import com.fs.his.vo.FsDoctorExtractListSVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 医生提现管理Service业务层处理
 *
 * @author fs
 * @date 2023-06-25
 */
@Service
public class FsDoctorExtractServiceImpl implements IFsDoctorExtractService
{
    @Autowired
    private FsDoctorExtractMapper fsDoctorExtractMapper;

    @Autowired
    private FsDoctorBillMapper fsDoctorBillMapper;

    @Autowired
    private FsDoctorMapper fsDoctorMapper;
    /**
     * 查询医生提现管理
     *
     * @param extractId 医生提现管理主键
     * @return 医生提现管理
     */
    @Override
    public FsDoctorExtract selectFsDoctorExtractByExtractId(String extractId)
    {
        return fsDoctorExtractMapper.selectFsDoctorExtractByExtractId(extractId);
    }

    /**
     * 查询医生提现管理列表
     *
     * @param fsDoctorExtract 医生提现管理
     * @return 医生提现管理
     */
    @Override
    public List<FsDoctorExtract> selectFsDoctorExtractList(FsDoctorExtract fsDoctorExtract)
    {
        return fsDoctorExtractMapper.selectFsDoctorExtractList(fsDoctorExtract);
    }

    /**
     * 新增医生提现管理
     *
     * @param fsDoctorExtract 医生提现管理
     * @return 结果
     */
    @Override
    public int insertFsDoctorExtract(FsDoctorExtract fsDoctorExtract)
    {
        fsDoctorExtract.setCreateTime(DateUtils.getNowDate());
        return fsDoctorExtractMapper.insertFsDoctorExtract(fsDoctorExtract);
    }

    /**
     * 修改医生提现管理
     *
     * @param fsDoctorExtract 医生提现管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateFsDoctorExtract(FsDoctorExtract fsDoctorExtract)
    {
        if (fsDoctorExtract==null)throw new CustomException("非法操作");

        FsDoctor fsDoctor = fsDoctorMapper.selectFsDoctorByDoctorId(fsDoctorExtract.getDoctorId());
        if (fsDoctor==null||fsDoctor.getStatus()==0){
            throw new CustomException("医生不存在或者已被禁用");
        }
        if (fsDoctorExtract.getStatus()==1){
            //提现
        }
        if(fsDoctorExtract.getStatus()==-1){
            FsDoctorBill fsDoctorBill = new FsDoctorBill();
            fsDoctorBill.setDoctorId(fsDoctorExtract.getDoctorId());
            fsDoctorBill.setBillType(3);
            fsDoctorBill.setTitle("提现申请被拒绝");
            fsDoctorBill.setMoney(fsDoctorExtract.getExtractPrice());
            fsDoctorBill.setBalance(fsDoctor.getBalance().add(fsDoctorExtract.getExtractPrice()));
            fsDoctorBill.setCreateTime(DateUtils.getNowDate());
            fsDoctorBillMapper.insertFsDoctorBill(fsDoctorBill);
            FsDoctor doctor = new FsDoctor();
            doctor.setDoctorId(fsDoctor.getDoctorId());
            doctor.setBalance(fsDoctor.getBalance().add(fsDoctorExtract.getExtractPrice()));
            fsDoctorMapper.updateFsDoctor(doctor);

        }
        FsDoctorExtract f = new FsDoctorExtract();
        f.setUpdateTime(DateUtils.getNowDate());
        f.setFailTime(DateUtils.getNowDate());
        f.setStatus(fsDoctorExtract.getStatus());
        f.setFailMsg(fsDoctorExtract.getFailMsg());
        return fsDoctorExtractMapper.updateFsDoctorExtract(fsDoctorExtract);
    }

    /**
     * 批量删除医生提现管理
     *
     * @param extractIds 需要删除的医生提现管理主键
     * @return 结果
     */
    @Override
    public int deleteFsDoctorExtractByExtractIds(String[] extractIds)
    {
        return fsDoctorExtractMapper.deleteFsDoctorExtractByExtractIds(extractIds);
    }

    /**
     * 删除医生提现管理信息
     *
     * @param extractId 医生提现管理主键
     * @return 结果
     */
    @Override
    public int deleteFsDoctorExtractByExtractId(String extractId)
    {
        return fsDoctorExtractMapper.deleteFsDoctorExtractByExtractId(extractId);
    }

    @Override
    public List<FsDoctorExtractListSVO> selectFsDoctorExtractListSVO(FsDoctorExtractListSParam param) {
        return fsDoctorExtractMapper.selectFsDoctorExtractListSVO(param);
    }
}
