package com.kiisoo.ic.activity.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.kiisoo.ic.activity.bean.ActivityScheduleVO;
import com.kiisoo.ic.activity.entity.PmnActivitySchedule;
import com.kiisoo.ic.activity.mapper.PmnActivityInstanceDOMapper;
import com.kiisoo.ic.activity.mapper.PmnActivityScheduleDOMapper;
import com.kiisoo.ic.activity.mapper.PmnActivityStoreRelationMapper;
import com.kiisoo.ic.activity.service.IPmnActivityInstanceService;
import com.kiisoo.ic.activity.service.IPmnActivityScheduleService;
import com.kiisoo.ic.activity.service.IPmnActivityStoreRelationService;
import com.kiisoo.ic.common.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

@RestController
@Api(value = "后台-活动池管理", tags = {"后台-活动池管理"})
@RequestMapping(value = "/activity/schedule")
@Slf4j
public class ActivityScheduleController extends BaseController {
    @Resource
    PmnActivityScheduleDOMapper pmnActivityScheduleDOMapper;
    @Resource
    PmnActivityInstanceDOMapper pmnActivityInstanceDOMapper;
    @Resource
    PmnActivityStoreRelationMapper pmnActivityStoreRelationMapper;
    @Resource
    IPmnActivityStoreRelationService pmnActivityStoreRelationService;
    @Resource
    IPmnActivityInstanceService pmnActivityInstanceService;
    @Resource
    IPmnActivityScheduleService pmnActivityScheduleService;

    @ApiOperation(value = "所有活动池列表")
    @GetMapping(value = "/list/all")
    public Map<String, Object> listAll(String categoryCode) {
        try {
            LambdaQueryWrapper<PmnActivitySchedule> wrapper = null;
            if(categoryCode != null) {
                wrapper = Wrappers.<PmnActivitySchedule>lambdaQuery().eq(PmnActivitySchedule::getCategoryCode, categoryCode);
            }
            List<PmnActivitySchedule> list = pmnActivityScheduleDOMapper.selectList(wrapper);
            return data(list);
        } catch (Exception e) {
            log.error("查询出错", e);
            return fail();
        }
    }
    @ApiOperation(value = "所有活动池分页")
    @GetMapping(value = "/list/page")
    public Map<String, Object> listPage(String categoryCode) {
        try {
            LambdaQueryWrapper<PmnActivitySchedule> wrapper = null;
            if(categoryCode != null) {
                wrapper = Wrappers.<PmnActivitySchedule>lambdaQuery().eq(PmnActivitySchedule::getCategoryCode, categoryCode);
            }
            List<PmnActivitySchedule> list = pmnActivityScheduleDOMapper.selectList(wrapper);
            return data(list);
        } catch (Exception e) {
            log.error("查询出错", e);
            return fail();
        }
    }

    @ApiOperation(value = "新增或修改活动池")
    @PostMapping(value = "/save")
    public Map<String, Object> save(@RequestBody @Validated ActivityScheduleVO activityScheduleVO) {
        try {
            boolean state = pmnActivityScheduleService.saveVO(activityScheduleVO);
            return data(state);
        } catch (Exception e) {
            log.error("查询出错", e);
            return fail();
        }
    }

    @ApiOperation("获取活动池详情")
    @GetMapping("/detail")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "活动池id", dataType = "int", paramType = "form"),
    })
    public Map<String, Object> detail(Long id){
        try {
            return data(pmnActivityScheduleService.getVO(id));
        } catch (Exception e) {
            log.error("查询出错", e);
            return fail();
        }
    }

    @ApiOperation("删除活动池")
    @GetMapping("/delete")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "活动池id", dataType = "int", paramType = "form"),
    })
    public Map<String, Object> delete(Long id){
        // TODO 删除之前需要做关联性检查，如果已经关联了活动，则不允许删除（软删除）
        return data(null);
    }
}
