package com.yonge.cooleshow.student.controller;

import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.dto.search.MemberPriceSettingsSearch;
import com.yonge.cooleshow.biz.dal.entity.MemberPriceSettings;
import com.yonge.cooleshow.biz.dal.entity.SysConfig;
import com.yonge.cooleshow.biz.dal.service.ActivityPlanService;
import com.yonge.cooleshow.biz.dal.service.MemberPriceSettingsService;
import com.yonge.cooleshow.biz.dal.service.SysConfigService;
import com.yonge.cooleshow.biz.dal.vo.ActivityPlanVo;
import com.yonge.cooleshow.biz.dal.vo.MemberPriceSettingsVo;
import com.yonge.cooleshow.biz.dal.vo.MemberPriceVo;
import com.yonge.cooleshow.biz.dal.wrapper.MemberPriceSettingsWrapper;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("${app-config.url.student:}/memberPriceSettings")
@Api(tags = "会员价格设置")
@RestController
public class MemberPriceSettingsController extends BaseController {
	@Resource
	private MemberPriceSettingsService memberPriceSettingsService;
	@Resource
	private SysConfigService sysConfigService;
	@Resource
	private ActivityPlanService activityPlanService;

	@Autowired
	private SysUserFeignService sysUserFeignService;

	@GetMapping("/detail/{id}")
	@ApiOperation(value = "详情", notes = "传入id")
	public HttpResponseResult<MemberPriceSettingsVo> detail(@PathVariable("id") Long id) {
		return succeed(memberPriceSettingsService.detail(id));
	}

	@PostMapping("/list")
	@ApiOperation(value = "查询列表")
	public HttpResponseResult<MemberPriceVo> list(@RequestBody MemberPriceSettingsSearch query) {
		query.setStatus(true);
		MemberPriceVo memberPriceVo = memberPriceSettingsService.getVipShare(query);
		return succeed(memberPriceVo);
	}

    @GetMapping("/check/{id}")
    @ApiOperation(value = "检查会员是否有效", notes = "传入id")
    public HttpResponseResult<Boolean> check(@PathVariable("id") Long id) {
        MemberPriceSettingsVo detail = memberPriceSettingsService.detail(id);
        if (detail == null) {
            return succeed(false);
        }else if (Boolean.FALSE.equals(detail.getStatus())) {
            return succeed(false);
        }
        return succeed(true);
    }

    @PostMapping("/checkChange")
    @ApiOperation(value = "检查会员是否有效", notes = "传入id")
    public HttpResponseResult<Boolean> checkChange(@RequestBody @Validated MemberPriceSettingsWrapper.MemberChange memberChange) {
        MemberPriceSettingsVo detail = memberPriceSettingsService.detail(memberChange.getId());
        if (detail == null) {
            return succeed(false);
        }else if (Boolean.FALSE.equals(detail.getStatus())) {
            return succeed(false);
        } else if (memberChange.getSalePrice() != null && memberChange.getSalePrice().compareTo(detail.getSalePrice()) !=0) {
            return succeed(false);
        }
        return succeed(true);
    }

	@PostMapping("/vipPermissions")
	@ApiOperation(value = "查询vip权限")
	public HttpResponseResult<List<SysConfig>> vipPermissions() {
		Map<String,Object> params = new HashMap<>();
		params.put("group", "VIP_PERMISSION");
		List<SysConfig> configs = sysConfigService.findAll(params);
		return succeed(configs);
	}

	@ApiOperation(value = "获取进行中的会员买赠活动")
	@PostMapping("/getMemberBuyGift")
	public HttpResponseResult<ActivityPlanVo> getMemberBuyGift() {
		return succeed(activityPlanService.getMemberBuyGift());
	}

	@ApiOperation(value = "获取双十二活动")
	@GetMapping("/getDoubleTwelve/{id}")
	public HttpResponseResult<ActivityPlanVo> getDoubleTwelve(@PathVariable("id") Long id) {
		return succeed(activityPlanService.detail(id));
	}

	@ApiOperation(value = "获取畅学卡")
	@GetMapping("/getDiscount")
	public HttpResponseResult<MemberPriceSettingsWrapper.UserDiscount> getDiscount() {
		SysUser user = sysUserFeignService.queryUserInfo();
		Long userId = null;
		if (user != null ){
			userId = user.getId();
		}
		return succeed(memberPriceSettingsService.getDiscount(userId));
	}


	@ApiOperation(value = "获取畅学卡")
	@GetMapping("/getDiscount/detail/{orderDetilId}")
	public HttpResponseResult<MemberPriceSettingsWrapper.UserDiscount> getDiscountByOrder(@PathVariable("orderDetilId") Long orderDetilId) {
		SysUser user = sysUserFeignService.queryUserInfo();
		Long userId = null;
		if (user != null ){
			userId = user.getId();
		}
		return succeed(memberPriceSettingsService.getDiscountByOrder(userId,orderDetilId));
	}


}
