package com.yc.ship.module.otc.service.storetradelog;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.framework.mybatis.core.util.MyBatisUtils;
import com.yc.ship.module.otc.controller.admin.storetradelog.vo.StoreTradeLogPageReqVO;
import com.yc.ship.module.otc.controller.admin.storetradelog.vo.StoreTradeLogRespVO;
import com.yc.ship.module.otc.controller.admin.storetradelog.vo.StoreTradeLogSaveReqVO;
import com.yc.ship.module.otc.dal.dataobject.storetradelog.StoreTradeLogDO;
import com.yc.ship.module.otc.dal.mysql.storetradelog.StoreTradeLogMapper;
import com.yc.ship.module.otc.enums.TradeModeEnum;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static com.yc.ship.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yc.ship.module.otc.enums.ErrorCodeConstants.STORE_TRADE_LOG_NOT_EXISTS;

/**
 * 旅行社门店交易记录：余额充值、预付购票、预付退款、余额购票 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class StoreTradeLogServiceImpl implements StoreTradeLogService {

    @Resource
    private StoreTradeLogMapper storeTradeLogMapper;

    @Override
    public Long createStoreTradeLog(StoreTradeLogSaveReqVO createReqVO) {
        // 插入
        StoreTradeLogDO storeTradeLog = BeanUtils.toBean(createReqVO, StoreTradeLogDO.class);
        storeTradeLog.setId(IdWorker.getId(storeTradeLog));
        storeTradeLogMapper.insert(storeTradeLog);
        // 返回
        return storeTradeLog.getId();
    }

    @Override
    public void updateStoreTradeLog(StoreTradeLogSaveReqVO updateReqVO) {
        // 校验存在
        validateStoreTradeLogExists(updateReqVO.getId());
        // 更新
        StoreTradeLogDO updateObj = BeanUtils.toBean(updateReqVO, StoreTradeLogDO.class);
        storeTradeLogMapper.updateById(updateObj);
    }

    @Override
    public void deleteStoreTradeLog(Long id) {
        // 校验存在
        validateStoreTradeLogExists(id);
        // 删除
        storeTradeLogMapper.deleteById(id);
    }

    @Override
    public int deleteStoreTradeLogById(Long id) {
        // 校验存在
        validateStoreTradeLogExists(id);
        return storeTradeLogMapper.deleteStoreTradeLogById(id);
    }

    private void validateStoreTradeLogExists(Long id) {
        if (storeTradeLogMapper.selectById(id) == null) {
            throw exception(STORE_TRADE_LOG_NOT_EXISTS);
        }
    }

    @Override
    public StoreTradeLogRespVO getStoreTradeLog(Long id) {
        return storeTradeLogMapper.getStoreTradeLogById(id);
    }

    @Override
    public List<StoreTradeLogDO> getStoreTradeLogByOrderId(Long storeId, String orderId) {
        return storeTradeLogMapper.selectList(StoreTradeLogDO::getOrderId, orderId, StoreTradeLogDO::getStoreId, storeId);
    }

    @Override
    public PageResult<StoreTradeLogDO> getStoreTradeLogPage(StoreTradeLogPageReqVO pageReqVO) {
        IPage<StoreTradeLogDO> iPage = MyBatisUtils.buildPage(pageReqVO);
        IPage<StoreTradeLogDO> result = storeTradeLogMapper.selectStorePage(iPage, pageReqVO);
        return new PageResult<>(result.getRecords(), result.getTotal());
    }

    @Override
    public StoreTradeLogDO getStoreTradeLog(Long orderId, TradeModeEnum tradeMode) {
        LambdaQueryWrapper<StoreTradeLogDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StoreTradeLogDO::getOrderId, orderId);
        queryWrapper.eq(StoreTradeLogDO::getTradeType, tradeMode.getType());
        List<StoreTradeLogDO> list = storeTradeLogMapper.selectList(queryWrapper);
        return list!=null && !list.isEmpty() ? list.get(0) : null;
    }

}
