package com.ruoyi.web.controller.api;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.ApiResult;
import com.ruoyi.mall.domain.Bo.OfflineServicesBo;
import com.ruoyi.mall.domain.Bo.OfflineServicesRegisterBo;
import com.ruoyi.mall.domain.OfflineServices;
import com.ruoyi.mall.domain.OfflineServicesRegister;
import com.ruoyi.mall.domain.Vo.OfflineServicesRegisterVo;
import com.ruoyi.mall.domain.Vo.OfflineServicesVo;
import com.ruoyi.mall.service.IOfflineServicesRegisterService;
import com.ruoyi.mall.service.IOfflineServicesService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

import static com.ruoyi.common.utils.PageUtils.startPage;

/**
 * @ClassName ApiOfflineServicesController
 * @Description 线下服务接口
 * @Author
 * @Date 2023/10/16
 * @Version 1.0
 **/
@RestController
@RequestMapping("/api/OfflineServices")
public class ApiOfflineServicesController extends BaseController {

    @Resource
    private  IOfflineServicesService offlineServicesService;
    @Resource
    private IOfflineServicesRegisterService offlineServicesRegisterService;

    /**
     * 线下服务列表
     */
    @GetMapping("/getOfflineServicesList")
    public AjaxResult getOfflineServicesList(@RequestParam(defaultValue = "1") Integer pageNum,
                                            @RequestParam(defaultValue = "10") Integer pageSize) {
        try{
            PageHelper.startPage(pageNum, pageSize);
            OfflineServices offlineServices = new OfflineServices();
            List<OfflineServices> list = offlineServicesService.selectOfflineServicesList(offlineServices);
            PageInfo<OfflineServices> pageInfo = new PageInfo<>(list);
            return AjaxResult.success().put("data",pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("线下服务列表异常");
        }
    }

    /**线下服务详情
     *
     * @param userId 用户id
     * @param serviceId 服务id
     * @return
     */
    @GetMapping("/getOfflineServiceDetail")
    public AjaxResult getOfflineServiceDetail(@NotNull(message = "offlineServices.getDetail.userId.isnull") Long userId,@NotNull(message = "offlineServices.getDetail.serviceId.isnull") Long serviceId) {
        try{
            //获取线下服务详情
            OfflineServices offlineServices = offlineServicesService.selectOfflineServicesByServiceId(serviceId);
            OfflineServicesVo reslut = new OfflineServicesVo();
            BeanUtil.copyProperties(offlineServices,reslut);
            //获取已报名列表
            OfflineServicesRegister offlineServicesRegister = new OfflineServicesRegister();
            offlineServicesRegister.setServiceId(serviceId);
            List<OfflineServicesRegisterVo> list = offlineServicesRegisterService.selectVoList(offlineServicesRegister);
            reslut.setRegisterList(list);
            //验证当前用户是否报名
            Boolean flag = list.stream()
                    .anyMatch(a -> a.getUserId() == userId);
            reslut.setRegisterFlag(flag);
            return AjaxResult.success().put("data",reslut);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取线下服务详情异常");
        }

    }

    /**报名
     *
     *
     * @return
     */
    @PostMapping("/register")
    public ApiResult register(@RequestBody OfflineServicesRegisterBo bo) {
        try {
            OfflineServicesRegister offlineServicesRegister = BeanUtil.toBean(bo,OfflineServicesRegister.class);
            List<OfflineServicesRegister> offlineServicesRegisters = offlineServicesRegisterService.selectOfflineServicesRegisterList(offlineServicesRegister);
            if(null != offlineServicesRegisters && offlineServicesRegisters.size()>0){

                return cancel(offlineServicesRegisters);
            }
            //增
            int i = offlineServicesRegisterService.insertOfflineServicesRegister(offlineServicesRegister);
            if (i > 0) {
                OfflineServices offlineServices = offlineServicesService.selectOfflineServicesByServiceId(bo.getServiceId());
                Long registerNum = offlineServices.getRegisterNum();
                offlineServices.setRegisterNum(registerNum + 1);
                offlineServicesService.updateOfflineServices(offlineServices);
                return ApiResult.ok("报名成功");
            } else {
                return ApiResult.error("报名失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResult.error("报名异常");
        }
    }
    /**取消报名*/
    private ApiResult cancel( List<OfflineServicesRegister> offlineServicesRegisters){
        try {
             OfflineServicesRegister   offlineServicesRegister = offlineServicesRegisters.get(0);
            //删
            int i = offlineServicesRegisterService.deleteOfflineServicesRegisterByRegisterId(offlineServicesRegister.getRegisterId());
            if (i > 0) {
                OfflineServices offlineServices = offlineServicesService.selectOfflineServicesByServiceId(offlineServicesRegister.getServiceId());
                Long registerNum = offlineServices.getRegisterNum();
                offlineServices.setRegisterNum(registerNum - 1);
                offlineServicesService.updateOfflineServices(offlineServices);
                return ApiResult.ok("取消报名成功");
            } else {
                return ApiResult.error("取消报名失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResult.error("取消报名异常");
        }
    }
}
