package com.ruoyi.frequency.bankcard.service.impl;

import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.page.PageResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.controller.req.BankCardAddReq;
import com.ruoyi.controller.req.BankCardEditReq;
import com.ruoyi.controller.req.BankCardPageReq;
import com.ruoyi.controller.resp.BankCardDetailResp;
import com.ruoyi.controller.resp.BankCardPageResp;
import com.ruoyi.response.ResponseData;
import com.ruoyi.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.code.PublicCommon;
import com.ruoyi.frequency.bankcard.mapper.BankCardMapper;
import com.ruoyi.frequency.bankcard.entity.BankCard;
import com.ruoyi.frequency.bankcard.service.BankCardService;

/**
 * 银行卡Service业务层处理
 *
 * @author liwenlong
 * @date 2024-04-10
 */
@Service
public class BankCardServiceImpl extends ServiceImpl<BankCardMapper, BankCard>  implements BankCardService
{
    @Autowired
    private BankCardMapper bankCardMapper;

    /**
     * 查询银行卡
     *
     * @param id 银行卡主键
     * @return 银行卡
     */
    @Override
    public BankCard selectBankCardById(Long id)
    {
        return bankCardMapper.selectBankCardById(id);
    }

    /**
     * 查询银行卡列表
     *
     * @param bankCard 银行卡
     * @return 银行卡
     */
    @Override
    public List<BankCard> selectBankCardList(BankCard bankCard)
    {
        return bankCardMapper.selectBankCardList(bankCard);
    }

    /**
     * 新增银行卡
     *
     * @param bankCard 银行卡
     * @return 结果
     */
    @Override
    public int insertBankCard(BankCard bankCard)
    {
        bankCard.setCreateData();
        return bankCardMapper.insert(bankCard);
    }

    /**
     * 修改银行卡
     *
     * @param bankCard 银行卡
     * @return 结果
     */
    @Override
    public int updateBankCard(BankCard bankCard)
    {
        bankCard.setUpdateData();
        return bankCardMapper.updateBankCard(bankCard);
    }

    /**
     * 批量删除银行卡
     *
     * @param ids 需要删除的银行卡主键
     * @return 结果
     */
    @Override
    public int deleteBankCardByIds(Long[] ids,Integer delFlag)
    {
        this.lambdaUpdate().set(BankCard::getDelFlag,delFlag).in(BankCard::getId,ids).update();
        return ids.length;
    }

    @Override
    public ResponseData<PageResult<Page<BankCardPageResp>>> bankCardPage(BankCardPageReq req, UserVo userVo) {

        Page<BankCardPageResp> page = this.baseMapper.bankCardPage(new Page<>(req.getPageNo(),req.getPageSize()),req,userVo);
        return ResponseData.success(new PageResult<>(page));
    }

    @Override
    public ResponseData bankCardAdd(BankCardAddReq req, UserVo userVo) {
        BankCard bankCard = new BankCard();
        BeanUtil.copyProperties(req,bankCard);
        bankCard.setCreateData();
        bankCard.setUserId(userVo.getUserId())
                .setUserType(userVo.getUserType());
        this.save(bankCard);
        return ResponseData.success();
    }

    @Override
    public ResponseData bankCardEdit(BankCardEditReq req, UserVo userVo) {
        BankCard bankCard = new BankCard();
        BeanUtil.copyProperties(req,bankCard);
        bankCard.setUpdateData();
        this.updateById(bankCard);

        return ResponseData.success();
    }

    @Override
    public ResponseData<BankCardDetailResp> bankCardDetail(Long bankCardId, UserVo userVo) {
        BankCardDetailResp bankCardDetail =   this.baseMapper.bankCardDetail(bankCardId,userVo);
        return ResponseData.success(bankCardDetail);
    }

    @Override
    public ResponseData bankCardDel(Long bankCardId, UserVo userVo) {
        this.lambdaUpdate().set(BankCard::getDelFlag,PublicCommon.删除)
                .eq(BankCard::getUserId,userVo.getUserId())
                .eq(BankCard::getUserType,userVo.getUserType())
                .eq(BankCard::getId,bankCardId).update();
        return ResponseData.success();
    }
}
