package com.iformall.service.invest.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.iformall.domain.po.invest.InvestOperateRecordEntity;
import com.iformall.domain.vo.invest.InvestOperateRecordQuery;
import com.iformall.domain.vo.invest.InvestPageQuery;
import com.iformall.domain.vo.invest.InvestPageResult;
import com.iformall.domain.vo.invest.InvestUserContext;
import com.iformall.enums.EnumFollowType;
import com.iformall.mapper.InvestOperateRecordMapper;
import com.iformall.service.invest.InvestHelper;
import com.iformall.service.invest.InvestOperateRecordService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;


@Service
public class InvestOperateRecordServiceImpl extends InvestBaseServiceImpl<InvestOperateRecordMapper, InvestOperateRecordEntity> implements InvestOperateRecordService {

    @Override
    public boolean save(InvestOperateRecordEntity entity) {
        return super.saveAction(entity, this::checkBeforeSaveOrUpdate);
    }

    void checkBeforeSaveOrUpdate(InvestOperateRecordEntity entity) {
        InvestHelper.notNull(entity.getTbl());
        InvestHelper.notNull(entity.getTid());
        entity.setOperator(InvestUserContext.getUserId());
    }

    @Deprecated
    @Override
    public boolean update(InvestOperateRecordEntity entity, Wrapper<InvestOperateRecordEntity> updateWrapper) {
        notSupport();
        return false;
    }

    @Deprecated
    @Override
    public boolean saveOrUpdate(InvestOperateRecordEntity entity) {
        notSupport();
        return false;
    }

    @Deprecated
    @Override
    public boolean updateById(InvestOperateRecordEntity entity) {
        notSupport();
        return false;
    }

    @Override
    public InvestPageResult<InvestOperateRecordEntity> selectPageByTid(InvestOperateRecordQuery recordQuery) {
        if (Objects.equals(recordQuery.getRecordType(), EnumFollowType.DEMAND)) {
            InvestPageResult<InvestOperateRecordEntity> pageResult = new InvestPageResult<>();
            long count = super.baseMapper.selectCustomerPageCount(recordQuery.getTid());
            if (count == 0) {
                return pageResult;
            }
            pageResult.setTotal(count);
            pageResult.setSize(recordQuery.getPageSize());
            pageResult.setCurrent(recordQuery.getPageNum());
            Integer limitStart = (recordQuery.getPageNum() - 1) * recordQuery.getPageSize();
            Integer limitSize = recordQuery.getPageSize();
            List data = super.baseMapper.selectCustomerPageByTid(recordQuery.getTid(), limitStart, limitSize);
            pageResult.setRecords(data);
            return pageResult;
        } else {
            InvestOperateRecordEntity params = new InvestOperateRecordEntity();
            params.setTid(recordQuery.getTid());
            params.setTbl(InvestOperateRecordEntity.TLB_TASK);
            InvestPageQuery<InvestOperateRecordEntity> pageQuery = new InvestPageQuery<>(params);
            BeanUtils.copyProperties(recordQuery, pageQuery);
            return this.queryPage(pageQuery);
        }
    }
}
