package cn.iocoder.yudao.module.custom.service.impl;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
import cn.iocoder.yudao.module.custom.entity.CustomerBalanceChange;
import cn.iocoder.yudao.module.custom.mapper.CustomerBalanceChangeMapper;
import cn.iocoder.yudao.module.custom.service.CustomerBalanceChangeService;
import cn.iocoder.yudao.module.custom.vo.CustomerBalanceChangePageReqVO;
import cn.iocoder.yudao.module.custom.vo.CustomerBalanceChangeRespVO;
import cn.iocoder.yudao.module.custom.vo.CustomerBalanceChangeSaveReqVO;
import cn.iocoder.yudao.module.member.entity.MemberCoupon;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import static cn.iocoder.yudao.common.BaseConstants.BALANCE_CHANGE_MAIN_KQ;
import static cn.iocoder.yudao.common.BaseConstants.BALANCE_CHANGE_TYPE_ZS;

/**
 * 用户积分（余额）、卡券变动记录 Service 实现类
 *
 * @author pqz
 */
@Service
@Validated
public class CustomerBalanceChangeServiceImpl extends ServiceImpl<CustomerBalanceChangeMapper, CustomerBalanceChange> implements CustomerBalanceChangeService {

    @Resource
    private CustomerBalanceChangeMapper customerBalanceChangeMapper;


    /**
     * @param cusId         客户id
     * @param userId        用户id
     * @param couponId      关联卡券id
     * @param changeType    变动类型
     * @param changeMain    变动主体
     * @param changeRule    变动规则
     * @param changeBalance 变动积分/次数
     * @param remBalance    变动后剩余积分/次数
     * @param reason        变动原因
     * @author PQZ
     * @date 21:54 2024/9/20
     **/
    @Override
    public void saveChange(String cusId, Long userId, String couponId, String changeType, String changeMain, String changeRule, BigDecimal changeBalance, BigDecimal remBalance, String reason) {
        CustomerBalanceChange change = initChange(cusId,userId,couponId,changeType,changeMain,changeRule,changeBalance,remBalance,reason);
        save(change);
    }

    /**
     * 初始化变动记录
     *
     * @param cusId         客户id
     * @param userId        用户id
     * @param couponId      关联卡券id
     * @param changeType    变动类型
     * @param changeMain    变动主体
     * @param changeRule    变动规则
     * @param changeBalance 变动积分/次数
     * @param remBalance    变动后剩余积分/次数
     * @param reason        变动原因
     * @return cn.iocoder.yudao.module.custom.entity.CustomerBalanceChange
     * @author PQZ
     * @date 10:09 2024/9/21
     **/
    @Override
    public CustomerBalanceChange initChange(String cusId, Long userId, String couponId, String changeType, String changeMain, String changeRule, BigDecimal changeBalance, BigDecimal remBalance, String reason) {
        CustomerBalanceChange change = new CustomerBalanceChange();
        change.setCusId(cusId);
        change.setUserId(userId);
        change.setCouponId(couponId);
        change.setChangeType(changeType);
        change.setChangeMain(changeMain);
        change.setChangeRule(changeRule);
        change.setChangeBalance(changeBalance);
        change.setRemBalance(remBalance);
        change.setReason(reason);
        return change;
    }


    /**
     * 获得用户积分（余额）、卡券变动记录分页
     *
     * @param pageReqVO 分页查询
     * @param page      分页参数
     * @return 用户积分（余额）、卡券变动记录分页
     */
    @Override
    public IPage<CustomerBalanceChangeRespVO> queryListPage(CustomerBalanceChangePageReqVO pageReqVO, Page<CustomerBalanceChangeRespVO> page) {
        return customerBalanceChangeMapper.queryListPage(pageReqVO, page);
    }

    /**
     * 转换变动记录
     *
     * @param cusId      客户id
     * @param userId     用户id
     * @param activeName 活动名称
     * @param couponList 卡券集合
     * @return java.util.List<cn.iocoder.yudao.module.custom.entity.CustomerBalanceChange>
     * @author PQZ
     * @date 21:58 2024/9/24
     **/
    @Override
    public List<CustomerBalanceChange> transChange(String cusId, Long userId, String activeName, List<MemberCoupon> couponList) {
        List<CustomerBalanceChange> transList = new ArrayList<>();
        couponList.forEach(item -> {
            CustomerBalanceChange change = initChange(cusId,
                    userId, item.getId(), BALANCE_CHANGE_TYPE_ZS, BALANCE_CHANGE_MAIN_KQ,
                    item.getOutRule(), item.getAmount(), item.getAmount(), "参加" + activeName + "活动领取");
            transList.add(change);
        });
        return transList;
    }

    /**
     * 通过用户id和卡券id查询积分、卡券的变动记录
     *
     * @param cusId    用户id
     * @param couponId 卡券id
     * @return java.util.List<cn.iocoder.yudao.module.custom.entity.CustomerBalanceChange>
     * @author PQZ
     * @date 22:48 2024/9/20
     **/
    @Override
    public List<CustomerBalanceChange> getCouponChange(String cusId, String couponId) {
        LambdaQueryWrapper<CustomerBalanceChange> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CustomerBalanceChange::getCusId,cusId)
                .eq(CustomerBalanceChange::getCouponId,couponId)
                .eq(BaseDO::getDeleted,'0');
        return list(lambdaQueryWrapper);
    }


}