package com.ym.mec.web.controller;

import com.ym.mec.biz.dal.dto.ActivityPlanAddDto;
import com.ym.mec.biz.dal.entity.ActivityPlan;
import com.ym.mec.biz.dal.page.ActivityPlanQueryInfo;
import com.ym.mec.biz.service.ActivityPlanService;
import com.ym.mec.biz.service.OrganizationService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.exception.BizException;
import com.yonge.log.model.AuditLogAnnotation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

@Api(tags = "活动方案")
@RequestMapping("activityPlan")
@RestController
public class ActivityPlanController extends BaseController {

    @Autowired
    private ActivityPlanService activityPlanService;
	@Autowired
	private OrganizationService organizationService;

    @ApiOperation(value = "新增活动方案")
    @PostMapping("/addVipGroupActivity")
    @PreAuthorize("@pcs.hasPermissions('activityPlan/addActivityPlan')")
    public Object addActivityPlan(@RequestBody ActivityPlanAddDto activityPlanAddDto){
        activityPlanService.addActivityPlan(activityPlanAddDto);
        return succeed();
    }

    @ApiOperation(value = "修改活动方案")
    @PostMapping("/updateActivityPlan")
    @PreAuthorize("@pcs.hasPermissions('activityPlan/updateActivityPlan')")
    public Object updateActivityPlan(@RequestBody ActivityPlanAddDto activityPlanAddDto){
        activityPlanService.updateActivityPlan(activityPlanAddDto);
        return succeed();
    }

    @ApiOperation(value = "修改活动方案")
    @PostMapping("/enable")
    @PreAuthorize("@pcs.hasPermissions('activityPlan/enable')")
    public Object enable(Integer id){
        ActivityPlan activityPlan = activityPlanService.get(id);
        activityPlan.setEnable(activityPlan.getEnable());
        activityPlanService.update(activityPlan);
        return succeed();
    }

    @ApiOperation(value = "分页查询活动方案")
    @GetMapping("/queryPage")
    @PreAuthorize("@pcs.hasPermissions('activityPlan/queryPage')")
    public Object queryPage(ActivityPlanQueryInfo queryInfo){
        queryInfo.setOrganId(organizationService.getEmployeeOrgan(queryInfo.getOrganId()));
        return succeed(activityPlanService.queryPage(queryInfo));
    }

    @ApiOperation(value = "删除活动方案")
    @PostMapping("/delete")
    @PreAuthorize("@pcs.hasPermissions('activityPlan/delete')")
    public Object delete(Integer id){
        if(Objects.isNull(id)){
            throw new BizException("请指定活动编号!");
        }
        activityPlanService.deleteActivityPlan(id);
        return succeed();
    }
}
