package com.fjhx.jushuitan.service.jst.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.fjhx.jushuitan.entity.jst.po.JstOrderDetail;
import com.fjhx.jushuitan.entity.jst.po.JstOrderInfo;
import com.fjhx.jushuitan.entity.jst.po.JstOrderPayInfo;
import com.fjhx.jushuitan.entity.jst.vo.JstOrderDetailVo;
import com.fjhx.jushuitan.mapper.jst.JstOrderInfoMapper;
import com.fjhx.jushuitan.service.jst.JstOrderDetailService;
import com.fjhx.jushuitan.service.jst.JstOrderInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.jushuitan.service.jst.JstOrderPayInfoService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fjhx.jushuitan.entity.jst.vo.JstOrderInfoVo;
import com.fjhx.jushuitan.entity.jst.dto.JstOrderInfoSelectDto;
import com.ruoyi.common.utils.wrapper.IWrapper;
import com.fjhx.jushuitan.entity.jst.dto.JstOrderInfoDto;
import cn.hutool.core.bean.BeanUtil;

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

/**
 * <p>
 * 聚水潭订单表 服务实现类
 * </p>
 *
 * @author 
 * @since 2024-01-20
 */
@Service
public class JstOrderInfoServiceImpl extends ServiceImpl<JstOrderInfoMapper, JstOrderInfo> implements JstOrderInfoService {
    @Resource
    JstOrderDetailService jstOrderDetailService;
    @Resource
    JstOrderPayInfoService jstOrderPayInfoService;

    @Override
    public List<JstOrderInfoVo> getList(JstOrderInfoSelectDto dto) {
        IWrapper<JstOrderInfo> wrapper = getWrapper();
        wrapper.orderByDesc("joi", JstOrderInfo::getId);
        List<JstOrderInfoVo> list = this.baseMapper.getList(wrapper);
        return list;
    }

    @Override
    public Page<JstOrderInfoVo> getPage(JstOrderInfoSelectDto dto) {
        IWrapper<JstOrderInfo> wrapper = getWrapper();
        if(ObjectUtil.isNotNull(dto.getStartTime())){
            wrapper.ge("joi", JstOrderInfo::getCreated, DateUtil.beginOfDay(dto.getStartTime()));
        }
        if(ObjectUtil.isNotNull(dto.getEndTime())){
            wrapper.le("joi", JstOrderInfo::getCreated, DateUtil.endOfDay(dto.getEndTime()));
        }
        wrapper.like("joi", JstOrderInfo::getOid, dto.getOid());
        wrapper.eq("joi", JstOrderInfo::getStatus, dto.getStatus());
        wrapper.orderByDesc("joi", JstOrderInfo::getId);
        Page<JstOrderInfoVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
        List<JstOrderInfoVo> records = page.getRecords();
        if (CollectionUtil.isNotEmpty(records)){
            records.forEach(item ->
                jstOrderPayInfoService.lambdaQuery().eq(JstOrderPayInfo::getOrderId, item.getId()).list().stream().findFirst().ifPresent(jstOrderPayInfo ->
                        item.setPayDate(jstOrderPayInfo.getPayDate())
                )
            );
        }
        page.setRecords(records);
        return page;
    }

    @Override
    public JstOrderInfoVo detail(Long id) {
        JstOrderInfo JstOrderInfo = this.getById(id);
        JstOrderInfoVo result = BeanUtil.toBean(JstOrderInfo, JstOrderInfoVo.class);

        List<JstOrderDetail> detailList = jstOrderDetailService.lambdaQuery().eq(JstOrderDetail::getOrderId, id).list();
        if (CollectionUtil.isNotEmpty(detailList)){
            result.setItems(BeanUtil.copyToList(detailList, JstOrderDetailVo.class));
        }
        return result;
    }

    @Override
    public void add(JstOrderInfoDto jstOrderInfoDto) {
        this.save(jstOrderInfoDto);
    }

    @Override
    public void edit(JstOrderInfoDto jstOrderInfoDto) {
        this.updateById(jstOrderInfoDto);
    }

    @Override
    public void delete(Long id) {

        this.removeById(id);
    }


    @Override
    public Long getLastTs() {
        JstOrderInfo last = lambdaQuery().orderByDesc(JstOrderInfo::getTs).last("limit 1").list().stream().findFirst().orElse(new JstOrderInfo());
        return last.getTs();
    }
}
