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.entity.VipGroupDefaultClassesUnitPrice;
import com.ym.mec.biz.service.VipGroupDefaultClassesUnitPriceService;
import com.ym.mec.common.controller.BaseController;
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.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@Api(tags = "vip课默认单价")
@RequestMapping("vipGroupDefaultClassesUnitPrice")
@RestController
public class VipGroupDefaultClassesUnitPriceController extends BaseController {

	@Autowired
	private VipGroupDefaultClassesUnitPriceService vipGroupDefaultClassesUnitPriceService;
	@Autowired
	private SysUserFeignService sysUserFeignService;

	@ApiOperation("单查询")
	@GetMapping(value = "/query")
    @PreAuthorize("@pcs.hasPermissions('vipGroupDefaultClassesUnitPrice/query')")
	public Object query(int id) {
		return succeed(vipGroupDefaultClassesUnitPriceService.get(id));
	}

	@ApiOperation("全查询")
	@GetMapping(value = "/queryAll")
    @PreAuthorize("@pcs.hasPermissions('vipGroupDefaultClassesUnitPrice/queryAll')")
	public Object queryAll(Long organId) {
		return succeed(vipGroupDefaultClassesUnitPriceService.findAll(null));
	}

	@ApiOperation("新增")
	@PostMapping(value = "/add")
    @PreAuthorize("@pcs.hasPermissions('vipGroupDefaultClassesUnitPrice/add')")
	public Object add(VipGroupDefaultClassesUnitPrice vipGroupDefaultClassesUnitPrice) {
		SysUser sysUser = sysUserFeignService.queryUserInfo();
		if(sysUser == null){
			return failed("获取用户信息失败");
		}
		Date date = new Date();
		vipGroupDefaultClassesUnitPrice.setCreateTime(date);
		vipGroupDefaultClassesUnitPrice.setUpdateTime(date);
//		if(Objects.isNull(vipGroupDefaultClassesUnitPrice.getOrganId())){
//			if(sysUser.getOrganId() != null){
//				vipGroupDefaultClassesUnitPrice.setOrganId(sysUser.getOrganId());
//			}
//		}
		vipGroupDefaultClassesUnitPriceService.insert(vipGroupDefaultClassesUnitPrice);
		return succeed();
	}

	@ApiOperation("修改")
	@PostMapping(value = "/update")
    @PreAuthorize("@pcs.hasPermissions('vipGroupDefaultClassesUnitPrice/update')")
	public Object update(VipGroupDefaultClassesUnitPrice vipGroupDefaultClassesUnitPrice) {
		Date date = new Date();
		vipGroupDefaultClassesUnitPrice.setUpdateTime(date);
		vipGroupDefaultClassesUnitPriceService.update(vipGroupDefaultClassesUnitPrice);
		return succeed();
	}

	@ApiOperation("删除")
	@PostMapping(value = "/delete")
    @PreAuthorize("@pcs.hasPermissions('vipGroupDefaultClassesUnitPrice/delete')")
	public Object delete(int id) {
		vipGroupDefaultClassesUnitPriceService.delete(id);
		return succeed();
	}

}
