package com.xinelu.manage.service.billinfo.impl;

import com.xinelu.common.enums.BillSourceEnum;
import com.xinelu.manage.domain.billinfo.BillInfo;
import com.xinelu.manage.dto.billinfo.BillInfoDto;
import com.xinelu.manage.mapper.billinfo.BillInfoMapper;
import com.xinelu.manage.mapper.phonedialrecord.PhoneDialRecordMapper;
import com.xinelu.manage.mapper.shortmessagesendrecord.ShortMessageSendRecordMapper;
import com.xinelu.manage.service.billinfo.IBillInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 账单信息Service业务层处理
 *
 * @author zh
 * @date 2026-04-16
 */
@Service
public class BillInfoServiceImpl implements IBillInfoService {
    @Resource
    private BillInfoMapper billInfoMapper;
    @Resource
    private PhoneDialRecordMapper phoneDialRecordMapper;
    @Resource
    private ShortMessageSendRecordMapper shortMessageSendRecordMapper;

    /**
     * 查询账单信息
     *
     * @param id 账单信息主键
     * @return 账单信息
     */
    @Override
    public BillInfo selectBillInfoById(Long id) {
        return billInfoMapper.selectBillInfoById(id);
    }

    /**
     * 查询账单信息列表
     *
     * @param billInfo 账单信息
     * @return 账单信息
     */
    @Override
    public List<BillInfo> selectBillInfoList(BillInfo billInfo) {
        return billInfoMapper.selectBillInfoList(billInfo);
    }

    /**
     * 新增账单信息
     *
     * @param billInfo 账单信息
     * @return 结果
     */
    @Override
    public int insertBillInfo(BillInfo billInfo) {
        billInfo.setCreateTime(LocalDateTime.now());
        return billInfoMapper.insertBillInfo(billInfo);
    }

    /**
     * 修改账单信息
     *
     * @param billInfo 账单信息
     * @return 结果
     */
    @Override
    public int updateBillInfo(BillInfo billInfo) {
        return billInfoMapper.updateBillInfo(billInfo);
    }

    /**
     * 批量删除账单信息
     *
     * @param ids 需要删除的账单信息主键
     * @return 结果
     */
    @Override
    public int deleteBillInfoByIds(Long[] ids) {
        return billInfoMapper.deleteBillInfoByIds(ids);
    }

    /**
     * 删除账单信息信息
     *
     * @param id 账单信息主键
     * @return 结果
     */
    @Override
    public int deleteBillInfoById(Long id) {
        return billInfoMapper.deleteBillInfoById(id);
    }

    /**
     * 账单明细信息列表
     *
     * @param code 账单编号
     * @return BillInfoDto
     */
    @Override
    public List<BillInfoDto> selectBillDetails(String code) {
        //查询总账单信息
        BillInfo billInfo = billInfoMapper.selectBillInfoByCode(code);
        if (Objects.isNull(billInfo) || StringUtils.isBlank(billInfo.getBillSource())) {
            return new ArrayList<>();
        }
        BillInfoDto billInfoDto = new BillInfoDto();
        billInfoDto.setPatientName("总计");
        billInfoDto.setPushLength(billInfo.getPushLength());
        billInfoDto.setExpense(billInfo.getBillExpense());
        List<BillInfoDto> bills = new ArrayList<>();
        //根据账单查询数据
        if (billInfo.getBillSource().equals(BillSourceEnum.TELEPHONE.getInfo())) {
            bills = phoneDialRecordMapper.selectPhoneDialStatisticByBillId(billInfo.getId());
            bills.add(billInfoDto);
        }
        if (billInfo.getBillSource().equals(BillSourceEnum.MESSAGE.getInfo())) {
            bills = shortMessageSendRecordMapper.selectShortMessageRecordByBillId(billInfo.getId());
            bills.add(billInfoDto);
        }
        return bills;
    }
}
