package com.fastbee.rechargecard.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.fastbee.common.utils.DateUtils;
import com.fastbee.rechargecard.domain.dto.UserConsumptionDetailsDto;
import com.fastbee.rechargecard.domain.dto.WeChatRecharge;
import com.fastbee.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fastbee.rechargecard.mapper.UserConsumptionDetailsMapper;
import com.fastbee.rechargecard.domain.UserConsumptionDetails;
import com.fastbee.rechargecard.service.IUserConsumptionDetailsService;

/**
 * 用户充值卡账单明细记录Service业务层处理
 *
 * @author kerwincui
 * @date 2024-12-18
 */
@Service
public class UserConsumptionDetailsServiceImpl implements IUserConsumptionDetailsService
{
    @Autowired
    private UserConsumptionDetailsMapper userConsumptionDetailsMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public List<UserConsumptionDetailsDto> selectUserConsumptionDetailsListByCardNumber(UserConsumptionDetails data) {
        List<UserConsumptionDetailsDto> result=new ArrayList<>();
        List<UserConsumptionDetails> value=userConsumptionDetailsMapper.selectUserConsumptionDetailsSwipListByCardNumber(data);
        for(int i=0;i<value.size();i++)
        {
            UserConsumptionDetails userConsumptionDetails=value.get(i);
            UserConsumptionDetailsDto temp=new UserConsumptionDetailsDto();
            if(userConsumptionDetails.getUserId()!=null)
            {
                temp.setUserName(sysUserMapper.selectUserById(userConsumptionDetails.getUserId()).getUserName());
            }
            temp.setId(userConsumptionDetails.getId());
            temp.setUserId(userConsumptionDetails.getUserId());
            temp.setDeviceNumber(userConsumptionDetails.getDeviceNumber());
            temp.setCardNumber(userConsumptionDetails.getCardNumber());
            temp.setProjectId(userConsumptionDetails.getProjectId());
            temp.setDeptId(userConsumptionDetails.getDeptId());
            temp.setBillingType(userConsumptionDetails.getBillingType());
            temp.setPumpTime(userConsumptionDetails.getPumpTime());
            temp.setUnitPrice(userConsumptionDetails.getUnitPrice());
            temp.setTotalPrice(userConsumptionDetails.getTotalPrice());
            temp.setDiscount(userConsumptionDetails.getDiscount());
            temp.setTaxAmount(userConsumptionDetails.getTaxAmount());
            temp.setAmountDue(userConsumptionDetails.getAmountDue());
            temp.setBillingPeriodDuration(userConsumptionDetails.getBillingPeriodDuration());
            temp.setBillingPeriodUnit(userConsumptionDetails.getBillingPeriodUnit());
            temp.setBillingDate(userConsumptionDetails.getBillingDate());
            temp.setStartTime(userConsumptionDetails.getStartTime());
            temp.setEndTime(userConsumptionDetails.getEndTime());
            temp.setPaymentMethod(userConsumptionDetails.getPaymentMethod());
            temp.setPaymentStatus(userConsumptionDetails.getPaymentStatus());
            temp.setPaymentTime(userConsumptionDetails.getPaymentTime());
            temp.setStatus(userConsumptionDetails.getStatus());
            temp.setAreaCode(userConsumptionDetails.getAreaCode());

            result.add(temp);

        }
        return result;
    }

    /**
     * 根据卡号查询用户充值卡账单明细记录列表
     *
     * @param cardNumber 用户充值卡号
     * @return 用户充值卡账单明细记录
     */
    @Override
    public List<UserConsumptionDetails> selectUserConsumptionDetailsListById(String cardNumber)
    {
        List<UserConsumptionDetails> list = new LambdaQueryChainWrapper<>(userConsumptionDetailsMapper)
                .eq(UserConsumptionDetails::getCardNumber, cardNumber)
                .list();
        return list;
    }

    /**
     * 查询用户充值卡账单明细记录
     *
     * @param id 用户充值卡账单明细记录主键
     * @return 用户充值卡账单明细记录
     */
    @Override
    public UserConsumptionDetails selectUserConsumptionDetailsById(Long id)
    {
        return userConsumptionDetailsMapper.selectUserConsumptionDetailsById(id);
    }

    /**
     * 查询用户充值卡账单明细记录列表
     *
     * @param userConsumptionDetails 用户充值卡账单明细记录
     * @return 用户充值卡账单明细记录
     */
    @Override
    public List<UserConsumptionDetails> selectUserConsumptionDetailsList(UserConsumptionDetails userConsumptionDetails)
    {
        return userConsumptionDetailsMapper.selectUserConsumptionDetailsList(userConsumptionDetails);
    }

    @Override
    public List<UserConsumptionDetailsDto> selectUserConsumptionDetailsShowList(UserConsumptionDetails data)
    {
        List<UserConsumptionDetailsDto> result=new ArrayList<>();
        List<UserConsumptionDetails> value=userConsumptionDetailsMapper.selectUserConsumptionDetailsList(data);
        for(int i=0;i<value.size();i++)
        {
            UserConsumptionDetails userConsumptionDetails=value.get(i);
            UserConsumptionDetailsDto temp=new UserConsumptionDetailsDto();
            if(userConsumptionDetails.getUserId()!=null)
            {
                temp.setUserName(sysUserMapper.selectUserById(userConsumptionDetails.getUserId()).getUserName());
            }

            temp.setId(userConsumptionDetails.getId());
            temp.setUserId(userConsumptionDetails.getUserId());
            temp.setDeviceNumber(userConsumptionDetails.getDeviceNumber());
            temp.setCardNumber(userConsumptionDetails.getCardNumber());
            temp.setProjectId(userConsumptionDetails.getProjectId());
            temp.setDeptId(userConsumptionDetails.getDeptId());
            temp.setBillingType(userConsumptionDetails.getBillingType());
            temp.setPumpTime(userConsumptionDetails.getPumpTime());
            temp.setUnitPrice(userConsumptionDetails.getUnitPrice());
            temp.setTotalPrice(userConsumptionDetails.getTotalPrice());
            temp.setDiscount(userConsumptionDetails.getDiscount());
            temp.setTaxAmount(userConsumptionDetails.getTaxAmount());
            temp.setAmountDue(userConsumptionDetails.getAmountDue());
            temp.setBillingPeriodDuration(userConsumptionDetails.getBillingPeriodDuration());
            temp.setBillingPeriodUnit(userConsumptionDetails.getBillingPeriodUnit());
            temp.setBillingDate(userConsumptionDetails.getBillingDate());
            temp.setStartTime(userConsumptionDetails.getStartTime());
            temp.setEndTime(userConsumptionDetails.getEndTime());
            temp.setPaymentMethod(userConsumptionDetails.getPaymentMethod());
            temp.setPaymentStatus(userConsumptionDetails.getPaymentStatus());
            temp.setPaymentTime(userConsumptionDetails.getPaymentTime());
            temp.setStatus(userConsumptionDetails.getStatus());
            temp.setAreaCode(userConsumptionDetails.getAreaCode());

            result.add(temp);

        }
        return result;
    }

    /**
     * 新增用户充值卡账单明细记录
     *
     * @param userConsumptionDetails 用户充值卡账单明细记录
     * @return 结果
     */
    @Override
    public int insertUserConsumptionDetails(UserConsumptionDetails userConsumptionDetails)
    {
        userConsumptionDetails.setCreateTime(DateUtils.getNowDate());
        return userConsumptionDetailsMapper.insertUserConsumptionDetails(userConsumptionDetails);
    }

    /**
     * 修改用户充值卡账单明细记录
     *
     * @param userConsumptionDetails 用户充值卡账单明细记录
     * @return 结果
     */
    @Override
    public int updateUserConsumptionDetails(UserConsumptionDetails userConsumptionDetails)
    {
        userConsumptionDetails.setUpdateTime(DateUtils.getNowDate());
        return userConsumptionDetailsMapper.updateUserConsumptionDetails(userConsumptionDetails);
    }

    /**
     * 批量删除用户充值卡账单明细记录
     *
     * @param ids 需要删除的用户充值卡账单明细记录主键
     * @return 结果
     */
    @Override
    public int deleteUserConsumptionDetailsByIds(Long[] ids)
    {
        return userConsumptionDetailsMapper.deleteUserConsumptionDetailsByIds(ids);
    }

    /**
     * 删除用户充值卡账单明细记录信息
     *
     * @param id 用户充值卡账单明细记录主键
     * @return 结果
     */
    @Override
    public int deleteUserConsumptionDetailsById(Long id)
    {
        return userConsumptionDetailsMapper.deleteUserConsumptionDetailsById(id);
    }

    @Override
    public int insertUserConsumptionDetailsWechat(WeChatRecharge recharge) {
        UserConsumptionDetails userConsumptionDetails=new UserConsumptionDetails();
        userConsumptionDetails.setCardNumber(String.valueOf(recharge.getCardnumber()));
        userConsumptionDetails.setBillingType(0);
        userConsumptionDetails.setAreaCode(String.valueOf(recharge.getAreacode()));
        userConsumptionDetails.setAmountDue(recharge.getAmount());
        userConsumptionDetails.setBillingDate(DateUtils.getNowDate());
        userConsumptionDetails.setPaymentStatus(0);
        userConsumptionDetails.setPaymentMethod(recharge.getType());
        userConsumptionDetails.setPaymentTime(DateUtils.getNowDate());
        userConsumptionDetails.setStatus(2);
        userConsumptionDetails.setCreateTime(DateUtils.getNowDate());
        userConsumptionDetails.setDeviceNumber(recharge.getDeviceNumber());
        return userConsumptionDetailsMapper.insertUserConsumptionDetails(userConsumptionDetails);

    }
}
