package com.jsowell.pile.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.jsowell.common.enums.ykc.ReturnCodeEnum;
import com.jsowell.common.exception.BusinessException;
import com.jsowell.common.util.DateUtils;
import com.jsowell.common.util.StringUtils;
import com.jsowell.pile.domain.MemberBasicInfo;
import com.jsowell.pile.domain.PileAuthCard;
import com.jsowell.pile.dto.CarVinDTO;
import com.jsowell.pile.service.IMemberBasicInfoService;
import com.jsowell.pile.vo.CarVinInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jsowell.pile.mapper.CarVinInfoMapper;
import com.jsowell.pile.domain.CarVinInfo;
import com.jsowell.pile.service.ICarVinInfoService;

/**
 * 车辆vin码Service业务层处理
 *
 * @author jsowell
 * @date 2023-06-07
 */
@Service
public class CarVinInfoServiceImpl implements ICarVinInfoService {
    @Autowired
    private CarVinInfoMapper carVinInfoMapper;

    @Autowired
    private IMemberBasicInfoService memberBasicInfoService;

    /**
     * 查询车辆vin码
     *
     * @param id 车辆vin码主键
     * @return 车辆vin码
     */
    @Override
    public CarVinInfo selectCarVinInfoById(Long id) {
        return carVinInfoMapper.selectCarVinInfoById(id);
    }

    /**
     * 查询车辆vin码列表
     *
     * @param carVinInfo 车辆vin码
     * @return 车辆vin码
     */
    @Override
    public List<CarVinInfo> selectCarVinInfoList(CarVinInfo carVinInfo) {
        return carVinInfoMapper.selectCarVinInfoList(carVinInfo);
    }

    /**
     * 新增车辆vin码
     *
     * @param carVinInfo 车辆vin码
     * @return 结果
     */
    @Override
    public int insertCarVinInfo(CarVinInfo carVinInfo) {
        carVinInfo.setCreateTime(DateUtils.getNowDate());
        return carVinInfoMapper.insertCarVinInfo(carVinInfo);
    }

    /**
     * 通过vin查询信息
     * @param vinCode
     * @return
     */
    @Override
    public CarVinInfo selectVinInfoByVin(String vinCode) {
        return carVinInfoMapper.selectVinInfoByVin(vinCode);
    }

    /**
     * 修改车辆vin码
     *
     * @param carVinInfo 车辆vin码
     * @return 结果
     */
    @Override
    public int updateCarVinInfo(CarVinInfo carVinInfo) {
        carVinInfo.setUpdateTime(DateUtils.getNowDate());
        return carVinInfoMapper.updateCarVinInfo(carVinInfo);
    }

    /**
     * 批量删除车辆vin码
     *
     * @param ids 需要删除的车辆vin码主键
     * @return 结果
     */
    @Override
    public int deleteCarVinInfoByIds(Long[] ids) {
        return carVinInfoMapper.deleteCarVinInfoByIds(ids);
    }

    /**
     * 删除车辆vin码信息
     *
     * @param id 车辆vin码主键
     * @return 结果
     */
    @Override
    public int deleteCarVinInfoById(Long id) {
        return carVinInfoMapper.deleteCarVinInfoById(id);
    }

    /**
     * 通过vin查询用户信息
     * @param vinCode
     * @return
     */
    @Override
    public CarVinInfoVO getMemberInfoByVinCode(String vinCode){
        CarVinInfoVO memberInfo = carVinInfoMapper.getMemberInfoByVinCode(vinCode);
        if (memberInfo == null) {
            return null;
        }
        memberInfo.setAccountBalance(memberInfo.getPrincipalBalance().add(memberInfo.getGiftBalance()).setScale(2, BigDecimal.ROUND_HALF_UP));
        return memberInfo;
    }

    /**
     * 通过memberId查询绑定的车辆vin列表
     * @param memberId
     * @return
     */
    @Override
    public List<CarVinInfoVO> getCarVinListByMemberId(String memberId) {
        return carVinInfoMapper.getCarVinListByMemberId(memberId);
    }

    /**
     * 用户绑定车辆vin
     * @param dto
     * @return
     */
    @Override
    public int bindAuthCard(CarVinDTO dto) {
        // String phoneNumber = dto.getPhoneNumber();
        // // 判断此用户是否已注册小程序账号
        // MemberBasicInfo memberBasicInfo = memberBasicInfoService.selectInfoByMobileNumber(phoneNumber);
        // if (memberBasicInfo == null) {
        //     throw new BusinessException(ReturnCodeEnum.CODE_USER_IS_NOT_REGISTER);
        // }
        // 判断此vin号码是否已被绑定
        CarVinInfo carVinInfo = carVinInfoMapper.selectVinInfoByVin(dto.getVinCode());
        if (carVinInfo != null) {
            throw new BusinessException(ReturnCodeEnum.CODE_SELECT_INFO_IS_NULL);
        }
        carVinInfo = new CarVinInfo();
        // 绑定操作
        carVinInfo.setVinCode(dto.getVinCode());
        carVinInfo.setStatus("1");
        carVinInfo.setMemberId(dto.getMemberId());
        carVinInfo.setCreateBy(dto.getMemberId());

        return carVinInfoMapper.insertCarVinInfo(carVinInfo);
    }

    /**
     * 用户解绑车辆vin信息
     * @param dto
     */
    @Override
    public int unbindCarVin(CarVinDTO dto) {
        // 根据传过来的vin号查询数据库
        CarVinInfo carVinInfo = selectVinInfoByVin(dto.getVinCode());
        if (carVinInfo == null) {
            throw new BusinessException(ReturnCodeEnum.CODE_SELECT_INFO_IS_NULL);
        }
        // 如果memberId对应的上，则进行解绑操作 (将数据库中 del_flag 改为 1)
        if (!StringUtils.equals(carVinInfo.getMemberId(), dto.getMemberId())) {
            throw new BusinessException(ReturnCodeEnum.CODE_SELECT_INFO_IS_NULL);
        }
        carVinInfo.setDelFlag("1");
        return updateCarVinInfo(carVinInfo);
    }


}
