package com.kiisoo.ic.activity.controller;

import com.kiisoo.ic.activity.bean.ActivityInstanceVO;
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.activity.service.IPmnPromotionSrcService;
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.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Map;

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

    @ApiOperation(value = "活动实例分页列表")
    @GetMapping(value = "/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "startDate", value = "开始时间,如2020-02-02", dataType = "string", paramType = "form"),
            @ApiImplicitParam(name = "endDate", value = "开始时间,如2020-02-02", dataType = "string", paramType = "form"),
            @ApiImplicitParam(name = "organizationId", value = "零售公司ID", dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "categoryCode", value = "categoryCode", dataType = "string", paramType = "form"),
            @ApiImplicitParam(name = "storeId", value = "店铺ID", dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "status", value = "状态,1、未开始 2、进行中 3、已结束 4、已取消", dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "pageNum", value = "当前页数", dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", defaultValue = "10", dataType = "int", paramType = "form"),
    })
    public Map<String, Object> instanceList(Long userId, String startDate, String endDate, Long organizationId, Long storeId, Integer status, String categoryCode, @RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize) {
        // TODO 需要根据当前用户角色分开查询
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return data(pmnActivityInstanceService.pageVO(userId, StringUtils.isBlank(startDate) ? null : sdf.parse(startDate), StringUtils.isBlank(endDate) ? null : sdf.parse(endDate), organizationId, storeId, status, categoryCode, pageNum, pageSize, null, null));
        } catch (Exception e) {
            log.error("查询出错", e);
            return fail();
        }
    }

    @ApiOperation(value = "店铺码分页列表")
    @GetMapping(value = "/StoreCode")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "instanceId", value = "活动实例ID", dataType = "string", paramType = "form"),
            @ApiImplicitParam(name = "pageNum", value = "当前页数", dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", defaultValue = "10", dataType = "int", paramType = "form"),
    })
    public Map<String, Object> storeCode(Long userId, Long instanceId, @RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize) {
        try {
            return data(pmnPromotionSrcService.pageStoreCode(userId, instanceId, pageNum, pageSize));
        } catch (Exception e) {
            log.error("查询出错", e);
            return fail();
        }
    }

    @ApiOperation(value = "根据活动池新建/编辑实例")
    @PostMapping(value = "/save")
    public Map<String, Object> saveInstance(@RequestBody @Validated ActivityInstanceVO activityInstanceVO) {
        try {
            boolean save = pmnActivityInstanceService.saveVO(activityInstanceVO, false);
            return save ? data(activityInstanceVO) : fail("操作失败");
        } catch (Exception e) {
            log.error("查询出错", e);
            return fail();
        }
    }

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

    @ApiOperation(value = "活动实例终止")
    @PostMapping(value = "/terminate")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "instanceId", value = "活动实例ID", dataType = "int", paramType = "form"),
    })
    public Map<String, Object> instanceTerminate(Long instanceId) {
        try {
            ActivityInstanceVO instanceVO = pmnActivityInstanceService.getVO(Long.valueOf(instanceId));
            log.info("调试: {}", instanceVO);
            boolean save = pmnActivityInstanceService.saveVO(instanceVO, true);
            return save ? success() : fail("操作失败");
        } 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) {
        try {
            boolean save = pmnActivityInstanceService.removeInstance(id);
            return save ? success() : fail("删除失败");
        } catch (Exception e) {
            log.error("查询出错", e);
            return fail();
        }
    }

    @ApiOperation("删除欢迎语")
    @GetMapping("/deleteWelcome")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "活动id", dataType = "int", paramType = "form"),
    })
    public Map<String, Object> deleteWelcome(Long id) {
        // TODO 删除之前需要做关联性检查，活动必须已经下线并终止，如果已经关联了店铺和导购，则不能删除
        try {
            boolean save = pmnActivityInstanceService.removeWelcome(id);
            return save ? success() : fail("删除失败");
        } catch (Exception e) {
            log.error("查询出错", e);
            return fail();
        }
    }
    @ApiOperation(value = "根据活动池新建/编辑实例")
    @PostMapping(value = "/saveWelcome")
    public Map<String, Object> saveWelcome(@RequestBody ActivityInstanceVO activityInstanceVO){
        try {
            boolean save = pmnActivityInstanceService.saveWelcome(activityInstanceVO);
            return save ? data(activityInstanceVO) : fail("操作失败");
        } catch (Exception e) {
            log.error("查询出错", e);
            return fail();
        }
    }

    @ApiOperation("删除活动")
    @GetMapping("/code/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "活动id", dataType = "int", paramType = "form"),
    })
    public Map<String, Object> codeList() {
        return data(null);
    }
}
