package com.heyu.api.controller.vv;


import com.alibaba.fastjson.JSON;
import com.heyu.api.alibaba.request.mm.VvAppLoginRequest;
import com.heyu.api.alibaba.request.vv.AppUserLoginRequest;
import com.heyu.api.common.annotation.Describe;
import com.heyu.api.data.dao.vv.VvBuyerDao;
import com.heyu.api.data.dto.BuyerDTO;
import com.heyu.api.data.dto.WeiChatLoginDTO;
import com.heyu.api.data.entity.vv.VvBuyerEntity;
import com.heyu.api.data.utils.R;
import com.heyu.api.data.utils.RedisUtils;
import com.heyu.api.data.utils.TokenUtils;
import com.heyu.api.data.utils.WechatUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/app/user")
public class AppUserLoginController {

    @Autowired
    private VvBuyerDao vvBuyerDao;

    @Autowired
    private RedisUtils redisUtils;


    // http://localhost:8888/app/user/login
    @RequestMapping("/login")
    @Describe("目标用户登录")
    public R login(@RequestBody AppUserLoginRequest request) {
        WeiChatLoginDTO weiChatLoginDTO = WechatUtils.getOpenId(request.getCode());
        if (weiChatLoginDTO == null) {
            return R.error("code 失效");
        }
        VvBuyerEntity target = vvBuyerDao.selectVvBuyerOpenId(weiChatLoginDTO.getOpenid());
        if (target == null) {
            target = new VvBuyerEntity();
            target.setOpenid(weiChatLoginDTO.getOpenid());
            target.setIsTest(0);
            target.setCreateTimestamp(System.currentTimeMillis());
        }
        target.setSessionKey(weiChatLoginDTO.getSessionKey());
        vvBuyerDao.insertOrUpdateVvBuyer(target);
        BuyerDTO buyerDTO = new BuyerDTO();
        buyerDTO.setBuyerId(target.getId());
        buyerDTO.setBuyerName(target.getBuyerName());
        buyerDTO.setFlag(target.getIsTest());
        String token = TokenUtils.generateToken(target.getId());
        buyerDTO.setToken(token);
        // 默认24 小时
        redisUtils.set(token, JSON.toJSON(buyerDTO), RedisUtils.DEFAULT_EXPIRE);
        return R.ok().put("buyer", buyerDTO);
    }


    // http://localhost:8888/app/user/target/login
    @RequestMapping("/target/login")
    @Describe("目标用户登录")
    public R targetLogin(@RequestBody VvAppLoginRequest request) {
        VvBuyerEntity vvBuyerEntity = vvBuyerDao.selectVvBuyerIsTestById(request.getBuyerId(), 1);
        if (vvBuyerEntity == null) {
            return R.error("你不是内部用户");
        }
        VvBuyerEntity target = vvBuyerDao.selectVvBuyerInfo(request.getTargetBuyer(),
                request.getTargetBuyer(),
                request.getTargetBuyer());

        BuyerDTO buyerDTO = new BuyerDTO();
        buyerDTO.setBuyerId(target.getId());
        buyerDTO.setBuyerName(target.getBuyerName());
        buyerDTO.setFlag(target.getIsTest());
        String token = TokenUtils.generateToken(target.getId());
        buyerDTO.setToken(token);
        // 默认24 小时
        redisUtils.set(token, JSON.toJSON(buyerDTO), RedisUtils.DEFAULT_EXPIRE);
        return R.ok().put("buyer", buyerDTO);
    }

}
