package co.yixiang.yshop.module.member.service.userbill;

import co.yixiang.yshop.framework.common.pojo.PageResult;
import co.yixiang.yshop.framework.mybatis.core.util.MyBatisUtils;
import co.yixiang.yshop.module.member.controller.admin.userbill.vo.UserBillPageReqVO;
import co.yixiang.yshop.module.member.controller.admin.userbill.vo.UserBillRespVO;
import co.yixiang.yshop.module.member.dal.dataobject.userbill.UserBillDO;
import co.yixiang.yshop.module.member.dal.mysql.userbill.UserBillMapper;
import co.yixiang.yshop.module.member.enums.BillEnum;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * 用户账单 Service 实现类
 *
 * @author yshop
 */
@Service
@Validated
public class UserBillServiceImpl implements UserBillService {

    @Resource
    private UserBillMapper userBillMapper;

    @Override
    public PageResult<UserBillDO> getUserBillPage(UserBillPageReqVO pageReqVO) {
        return userBillMapper.selectPage(pageReqVO);
    }

    @Override
    public PageResult<UserBillRespVO> getUserBillPageAdmin(UserBillPageReqVO pageReqVO) {
        // MyBatis Plus 查询
        IPage<UserBillRespVO> mpPage = MyBatisUtils.buildPage(pageReqVO);
        userBillMapper.selectPageAdmin(mpPage, pageReqVO);
        return new PageResult<>(mpPage.getRecords(), mpPage.getTotal());
    }


    /**
     * 增加支出流水
     *
     * @param uid      uid
     * @param title    账单标题
     * @param category 明细种类
     * @param type     明细类型
     * @param number   明细数字
     * @param balance  剩余
     * @param mark     备注
     */
    @Override
    public void expend(Long uid, String title, String category, String type, double number, double balance, String mark) {
        UserBillDO userBill = UserBillDO.builder()
                .uid(uid)
                .title(title)
                .category(category)
                .type(type)
                .number(BigDecimal.valueOf(number))
                .balance(BigDecimal.valueOf(balance))
                .mark(mark)
                .pm(BillEnum.PM_0.getValue())
                .build();

        userBillMapper.insert(userBill);
    }

    /**
     * 增加收入/支入流水
     *
     * @param uid      uid
     * @param title    账单标题
     * @param category 明细种类
     * @param type     明细类型
     * @param number   明细数字
     * @param balance  剩余
     * @param mark     备注
     * @param linkId   关联id
     */
    @Override
    public void income(Long uid, String title, String category, String type, BigDecimal number,
                       BigDecimal balance, String mark, String linkId) {
        UserBillDO userBill = UserBillDO.builder()
                .uid(uid)
                .title(title)
                .category(category)
                .type(type)
                .number(number)
                .balance(balance)
                .mark(mark)
                .pm(BillEnum.PM_1.getValue())
                .linkId(linkId)
                .build();

        userBillMapper.insert(userBill);
    }


    @Override
    public void addBillLog(Long uid, String title, String category, String type, BigDecimal number, BigDecimal balance, String mark, String linkId, Integer pm) {
        UserBillDO userBill = UserBillDO.builder()
                .uid(uid)
                .title(title)
                .category(category)
                .type(type)
                .number(number)
                .balance(balance)
                .mark(mark)
                .pm(pm)
                .build();

        userBillMapper.insert(userBill);
    }

    @Override
    public Long getUserBillCount(UserBillPageReqVO pageReqVO) {
        return userBillMapper.selectCount(pageReqVO);
    }


}
