package com.ym.mec.web.controller;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.dao.EmployeeDao;
import com.ym.mec.biz.dal.dto.VipGroupActivityAddDto;
import com.ym.mec.biz.dal.entity.Employee;
import com.ym.mec.biz.dal.page.VipGroupActivityQueryInfo;
import com.ym.mec.biz.service.VipGroupActivityService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.exception.BizException;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @Author Joburgess
 * @Date 2019/10/1
 */

@Api(tags = "vip课活动")
@RequestMapping("vipGroupActivity")
@RestController
public class VipGroupActivityController extends BaseController {

    @Autowired
    private VipGroupActivityService vipGroupActivityService;
    @Autowired
    private SysUserFeignService sysUserFeignService;
	@Autowired
	private EmployeeDao employeeDao;

    @ApiOperation(value = "新增vip课活动方案")
    @PostMapping("/addVipGroupActivity")
    @PreAuthorize("@pcs.hasPermissions('vipGroupActivity/addVipGroupActivity')")
    public Object addVipGroupActivity(@RequestBody VipGroupActivityAddDto vipGroupActivityAddDto){
        vipGroupActivityService.addVipGroupActivity(vipGroupActivityAddDto);
        return succeed();
    }

    @ApiOperation(value = "分页查询活动方案")
    @GetMapping("/queryPage")
    @PreAuthorize("@pcs.hasPermissions('vipGroupActivity/queryPage')")
    public Object queryPage(VipGroupActivityQueryInfo queryInfo){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if(sysUser == null){
            return failed("用户信息获取失败");
        }
		if (!sysUser.getIsSuperAdmin()) {
            Employee employee = employeeDao.get(sysUser.getId());
            if (StringUtils.isEmpty(queryInfo.getOrganId())) {
                queryInfo.setOrganId(employee.getOrganIdList());
            }else if(StringUtils.isEmpty(employee.getOrganIdList())){
                return failed("用户所在分部异常");
            }else {
                List<String> list = Arrays.asList(employee.getOrganIdList().split(","));
                if(!list.containsAll(Arrays.asList(queryInfo.getOrganId().split(",")))){
                    return failed("非法请求");
                }
            }
		}
        return succeed(vipGroupActivityService.queryPage(queryInfo));
    }

    @ApiOperation(value = "修改活动方案")
    @PostMapping("/update")
    @PreAuthorize("@pcs.hasPermissions('vipGroupActivity/update')")
    public Object update(@RequestBody VipGroupActivityAddDto vipGroupActivityAddDto){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if(sysUser == null){
            return failed("用户信息获取失败");
        }
        vipGroupActivityService.updateVipGroupActivity(vipGroupActivityAddDto, sysUser.getId());
        return succeed();
    }

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

    @ApiOperation(value = "根据课程类型获取对应课程活动方案")
    @GetMapping("/findByVipGroupCategory")
    @PreAuthorize("@pcs.hasPermissions('vipGroupActivity/findByVipGroupCategory')")
    public Object findByVipGroupCategory(Long categoryId, Integer teacherId){
        return succeed(vipGroupActivityService.findByVipGroupCategory(categoryId, teacherId));
    }

}
