package com.zbkj.service.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zbkj.common.exception.CrmebException;
import com.zbkj.common.model.activity.ActivityRegister;
import com.zbkj.common.model.game.Game;
import com.zbkj.common.model.place.PlaceRegister;
import com.zbkj.common.model.user.User;
import com.zbkj.common.page.CommonPage;
import com.zbkj.common.request.*;
import com.zbkj.common.response.ActivityRegisterResponse;
import com.zbkj.common.response.PlaceRegisterResponse;
import com.zbkj.common.vo.PlaceRegisterVo;
import com.zbkj.service.dao.PlaceRegisterDao;
import com.zbkj.service.service.PlaceRegisterService;
import com.zbkj.service.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

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

@Service
public class PlaceRegisterServiceImpl extends ServiceImpl<PlaceRegisterDao, PlaceRegister> implements PlaceRegisterService {

    @Resource private PlaceRegisterDao dao;

    @Autowired private UserService userService;

    @Override
    public Boolean subscribe(PlaceRegisterFrontRequest request) {
        Integer userId = userService.getUserIdException();
        LambdaQueryWrapper<PlaceRegister> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.select(PlaceRegister::getId);
        lambdaQueryWrapper.eq(PlaceRegister::getUid, userId);
        lambdaQueryWrapper.eq(PlaceRegister::getStatus, 0);
        List<PlaceRegister> list = dao.selectList(lambdaQueryWrapper);
        if (list.size() > 0) {
            throw new CrmebException("您目前存在正在审核的预约信息，请注意来电");
        }
        PlaceRegister add = JSON.parseObject(JSON.toJSONString(request), PlaceRegister.class);
        add.setUid(userId);
        add.setStatus(0);
        dao.insert(add);
        return true;
    }

    @Override
    public PageInfo<PlaceRegisterVo> getAdminList(PlaceRegisterSearchRequest request, PageParamRequest pageParamRequest) {
        Page<PlaceRegister> page = PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());
        LambdaQueryWrapper<PlaceRegister> lambdaQueryWrapper = Wrappers.lambdaQuery();
        if (!StringUtils.isBlank(request.getKeywords())) {
            lambdaQueryWrapper.and(i -> i
                    .or().like(PlaceRegister::getUserName, request.getKeywords())
                    .or().like(PlaceRegister::getCellPhone, request.getKeywords()));
        }
        if (null != request.getStatus()) {
            lambdaQueryWrapper.eq(PlaceRegister::getStatus, request.getStatus());
        }
        if (null != request.getBeginDate()) {
            lambdaQueryWrapper.ge(PlaceRegister::getSubscribeDate, request.getBeginDate());
        }
        if (null != request.getEndDate()) {
            lambdaQueryWrapper.le(PlaceRegister::getSubscribeDate, request.getEndDate());
        }
        lambdaQueryWrapper.eq(PlaceRegister::getIsShow, false);
        lambdaQueryWrapper.orderByAsc(PlaceRegister::getStatus).orderByDesc(PlaceRegister::getId);
        List<PlaceRegister> list = dao.selectList(lambdaQueryWrapper);
        List<PlaceRegisterVo> voList = JSON.parseArray(JSON.toJSONString(list), PlaceRegisterVo.class);
        return CommonPage.copyPageInfo(page, voList);
    }

    @Override
    public Boolean updateRegister(Integer id, PlaceRegisterRequest request) {
        PlaceRegister upd = JSON.parseObject(JSON.toJSONString(request), PlaceRegister.class);
        upd.setId(id);
        if (null != upd.getStatus() && upd.getStatus() == 2) { upd.setCancelData(new Date()); }
        dao.updateById(upd);
        return true;
    }

    @Override
    public PlaceRegister getDetail(Integer id) {
        PlaceRegister pr = getById(id);
        if (ObjectUtil.isNull(pr)) {
            throw new CrmebException("数据不存在");
        }
        return pr;
    }

    @Override
    public PageInfo<PlaceRegisterResponse> myRegisterList(PageParamRequest pageParamRequest) {
        User info = userService.getInfoException();
        Page<PlaceRegisterResponse> page = PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());
        List<PlaceRegisterResponse> list = dao.myRegisterList(info.getUid());
        return CommonPage.copyPageInfo(page, list);
    }

    @Override
    public Boolean cancel(PlaceCancelRequest request) {
        PlaceRegister pr = dao.selectById(Integer.parseInt(request.getPlaceActivityId()));
        if (null != pr && pr.getStatus() == 0) {
            PlaceRegister upd = new PlaceRegister();
            upd.setId(pr.getId());
            upd.setStatus(2);
            upd.setCancelReason(request.getCancelReason());
            dao.updateById(upd);
        } else {
            throw new CrmebException("当前状态不能取消了欧");
        }
        return true;
    }

    @Override
    public Boolean recordsDelete(Integer id) {
        PlaceRegister upd = new PlaceRegister();
        upd.setId(id);
        upd.setIsShow(1);
        dao.updateById(upd);
        return true;
    }
}
