package com.usky.park.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.usky.common.core.bean.CommonPage;
import com.usky.common.core.exception.BusinessException;
import com.usky.park.domain.DataStHistory;
import com.usky.park.domain.EventEg;
import com.usky.common.security.utils.SecurityUtils;
import com.usky.park.mapper.DataStHistoryMapper;
import com.usky.park.service.DataStHistoryService;
import com.usky.common.mybatis.core.AbstractCrudService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;


/**
 * <p>
 * 无人驿站历史数据 服务实现类
 * </p>
 *
 * @author fu
 * @since 2023-11-29
 */
@Service
public class DataStHistoryServiceImpl extends AbstractCrudService<DataStHistoryMapper, DataStHistory> implements DataStHistoryService {
    @Autowired
    private DataStHistoryMapper dataStHistoryMapper;

    /**
     * 包裹信息录入
     * @param dataStHistory
     * @param appKey
     */
    @Override
    public void add(DataStHistory dataStHistory, String appKey) {

        String appKeyLo = "6C57702BB93E9BA136B9337F0C1BA1BC";

        if (!appKey.equals(appKeyLo)) {
            throw new IllegalArgumentException("appKey不正确!");
        } else if ((dataStHistoryMapper.checkPackNo(dataStHistory.getPackNo())).size() > 0) {
            throw new BusinessException("订单编号已存在!请再次核对");
        } else {
            dataStHistory.setAppKey(appKey);
        }

        LocalDateTime dataTime = LocalDateTime.now();
        Integer packStatus = 1;
        dataStHistory.setDataTime(dataTime);
        dataStHistory.setPackStatus(packStatus);

        baseMapper.insert(dataStHistory);
    }

    /**
     * 分页查询包裹信息
     *
     * @param pickupStatus  包裹状态
     * @param pageNum   第几页
     * @param pageSize  页大小
     */
    @Override
    public CommonPage<DataStHistory> pageList(Integer pickupStatus, Integer pageNum, Integer pageSize){
        IPage<DataStHistory> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<DataStHistory> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(DataStHistory::getReceiverPhone,SecurityUtils.getLoginUser().getSysUser().getPhonenumber());
        if (pickupStatus != null) {
            if (pickupStatus == 0) {
                queryWrapper.eq(DataStHistory::getPickupStatus, 0);//代取件
            } else if (pickupStatus == 1) {
                queryWrapper.eq(DataStHistory::getPickupStatus, 1);//已取件
            }
        }
        queryWrapper.orderByAsc(DataStHistory::getPickupStatus);//pickupStatus为空就查询所有包裹信息
        page = this.page(page,queryWrapper);
        return new CommonPage<>(page.getRecords(),page.getTotal(),pageNum,pageSize);
    }
}
