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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.jushuitan.entity.jst.ThematicAnalysisBo;
import com.fjhx.jushuitan.entity.jst.dto.JstOrderInfoDto;
import com.fjhx.jushuitan.entity.jst.dto.JstOrderInfoSelectDto;
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.entity.jst.vo.JstOrderInfoVo;
import com.fjhx.jushuitan.mapper.jst.JstOrderInfoMapper;
import com.fjhx.jushuitan.service.jst.JstOrderDetailService;
import com.fjhx.jushuitan.service.jst.JstOrderInfoService;
import com.fjhx.jushuitan.service.jst.JstOrderPayInfoService;
import com.ruoyi.common.utils.wrapper.IWrapper;
import com.ruoyi.common.utils.wrapper.SqlField;
import org.springframework.stereotype.Service;

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

/**
 * <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);
        if (ObjectUtil.isNull(JstOrderInfo)) {
            throw new RuntimeException("订单不存在");
        }
        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);
    }

    @DSTransactional
    @Override
    public void edit(JstOrderInfoDto jstOrderInfoDto) {
        this.updateById(jstOrderInfoDto);
        jstOrderDetailService.updateBatchById(jstOrderInfoDto.getItems());
    }

    @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();
    }


    @Override
    public Page<ThematicAnalysisBo> thematicAnalysis(JstOrderInfoSelectDto dto) {
        IWrapper<Object> wrapper = IWrapper.getWrapper();
        //检索条件：订单状态、订单时间、关键字（内部订单号、店铺、订单类型）

        //订单状态
        wrapper.eq("joi.status", dto.getStatus());

        //订单时间-时间范围过滤
        wrapper.ge("joi.order_date", dto.getBeginTime());
        wrapper.le("joi.order_date", dto.getEndTime());

        //关键字（内部订单号、店铺、订单类型）
        wrapper.keyword(dto.getKeyword(),
                new SqlField("joi.oid"),
                new SqlField("joi.shop_name"),
                new SqlField("joi.type")
        );

        return baseMapper.thematicAnalysisPage(dto.getPage(), wrapper);
    }
}
