package com.qs.mp.web.controller.api.channel;

import cn.jsms.api.ValidSMSResult;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qs.mp.channel.domain.Channel;
import com.qs.mp.channel.domain.param.ChannelInviteParam;
import com.qs.mp.channel.service.IChannelService;
import com.qs.mp.common.core.domain.AjaxResult;
import com.qs.mp.common.core.domain.AjaxResult.Type;
import com.qs.mp.common.enums.ChannelRoleEnum;
import com.qs.mp.common.enums.UserIdentityEnum;
import com.qs.mp.common.jsms.JSMSUtils;
import com.qs.mp.framework.service.IWxUrlLinkService;
import com.qs.mp.framework.web.service.SysLoginService;
import com.qs.mp.utils.SecurityUtils;
import com.qs.mp.web.controller.common.BaseApiController;
import io.swagger.annotations.Api;
import java.util.Map;
import ma.glasnost.orika.MapperFacade;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @auther zhongcp
 * @create 2022-02-28 16:17:48
 * @describe 渠道邀请前端控制器
 */
@Api("渠道邀请API")
@RestController
@RequestMapping("/api/v1/mp/channel/*")
@Component
public class ChannelInviteController extends BaseApiController {

	@Autowired
	private IChannelService channelService;

	@Autowired
	private IWxUrlLinkService wxUrlLinkService;

	@Autowired
	private MapperFacade mapperFacade;

	@Value("${wx-channel.appId}")
	private String channelAppId;

	@Autowired
	private SysLoginService sysLoginService;

	/**
	 * 生成邀请码
	 *
	 * @return
	 */
	@PostMapping("invite/generate")
	public AjaxResult generateInviteCode(@RequestBody  JSONObject jsonObject) {
		Long channelId = SecurityUtils.getLoginUser().getChannelId();
		Channel channel = channelService.getById(channelId);
		if (StringUtils.isNotBlank(channel.getInvitePic())){
			return new AjaxResult(Type.SUCCESS, "", channel.getInvitePic());
		}
		String type = jsonObject.getString("type");
		String rst = wxUrlLinkService.generateCode("pages/login/invitation", "id=" + channelId + "&type=" + type, channelAppId);
		channel.setInvitePic(rst);
		channelService.updateById(channel);
		return new AjaxResult(Type.SUCCESS, "", rst);
	}

	/**
	 * 注册成为经销商
	 *
	 * @param
	 * @return
	 */
	@PostMapping(value = "invite/register")
	public AjaxResult register(@RequestBody ChannelInviteParam param) {

		Channel parent = channelService.getById(param.getChannelId());
		if(null == parent || parent.getLevel() == 0) {
			return error("渠道不存在");
		}
		ValidSMSResult res = JSMSUtils.sendValidSMSCode(param.getMessageId(), param.getCode());
		if (res == null || !res.getIsValid()) {
			return error("短信验证码错误");
		}
		int cnt = channelService.count(new LambdaQueryWrapper<Channel>().eq(Channel::getMobile, param.getMobile()));
		if (cnt > 0) {
			return error("手机号已存在");
		}
		// 创建经销商
		Channel channel = new Channel();
		channel.setChannelNo(parent.getChannelNo() + ".");
		//if (ChannelRoleEnum.CHANNEL.getValue().equals(param.getType())) {
			channel.setLevel(0);
		/*} else if (ChannelRoleEnum.SALESITE.getValue().equals(param.getType())) {
			channel.setLevel(parent.getLevel() + 1);
		}*/
		channel.setParentId(parent.getChannelId());
		channel.setMobile(param.getMobile());
		channel.setName(param.getMobile());
		channelService.saveChannel(channel,  ChannelRoleEnum.SALESITE);
		// 直接登录
		Map<String, String> result = sysLoginService.wxAuthLogin(param.getMobile(), UserIdentityEnum.CHANNEL.ordinal());
		AjaxResult ajax = AjaxResult.success();
		for (String key : result.keySet()) {
			ajax.put(key, result.get(key));
		}
		return ajax;
	}
}
