package com.kiisoo.ic.webappmy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kiisoo.ic.common.BaseController;
import com.kiisoo.ic.store.constant.Constants;
import com.kiisoo.ic.store.entity.PoiStoreStaff;
import com.kiisoo.ic.store.mapper.PoiStoreStaffDOMapper;
import com.kiisoo.ic.webappmy.vo.StafferInfoVO;
import com.kiisoo.ic.webappmy.vo.UserParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 *  小程序-我的功能请求控制器
 * Created by hc on 2020/4/16
 */
@RestController
@RequestMapping("/webapp/my")
@Slf4j
public class WebAppMyController extends BaseController {

    @Autowired
    private PoiStoreStaffDOMapper poiStoreStaffDOMapper;

    /**
     * 获取用户信息
     */
    @GetMapping("/get/staffer/info")
    public Map<String,Object> userInfo(long userId){
        try {
            List<StafferInfoVO> info = poiStoreStaffDOMapper.selectInfoById(userId);
            if(info.size() > 0){
                return data(info.get(0));
            }else {
                throw new Exception();
            }
        }catch (Exception e){
            log.error("获取用户信息异常",e);
            return fail();
        }
    }

    /**
     * 获取门店推广码
     */
    @GetMapping("/get/shop/qr")
    public Map<String,Object> shopIcQr(long shopId){
        try {
            PoiStoreStaff poiStoreStaff = new PoiStoreStaff();
            poiStoreStaff.setStoreId(shopId);
            poiStoreStaff.setType(Constants.TYPE_MANAGER);
            poiStoreStaff.setStatus(Constants.STATUS_USEFUL);
            String img = null;
            List<PoiStoreStaff> poiStoreStaffs = poiStoreStaffDOMapper.selectList(new QueryWrapper<>(poiStoreStaff));
            if(poiStoreStaffs.size() > 0){
                img = poiStoreStaffs.get(0).getEpWechatQrCode();
                return data(img);
            }else{
                throw new Exception();
            }
        }catch (Exception e){
            log.error("获取门店推广码异常",e);
            return fail();
        }
    }

    /**
     * 获取用户推广码
     */
    @GetMapping("/get/user/qr")
    public Map<String,Object> userIcQr(long userId){
        try {

            PoiStoreStaff poiStoreStaff = new PoiStoreStaff();
            poiStoreStaff.setUserId(userId);
            poiStoreStaff.setStatus(Constants.STATUS_USEFUL);
            String img = null;
            List<PoiStoreStaff> poiStoreStaffs = poiStoreStaffDOMapper.selectList(new QueryWrapper<>(poiStoreStaff));
            if(poiStoreStaffs.size() > 0){
                img = poiStoreStaffs.get(0).getEpWechatQrCode();
                return data(img);
            }else{
                throw new Exception();
            }
        }catch (Exception e){
            log.error("获取用户推广码异常",e);
            return fail();
        }
    }

    /**
     * 退出登入
     */
    @PostMapping(value = "/login/quit")
    public Map<String,Object> webAppUserQuitLogin(@RequestBody UserParam param){
        try {
            return data(param.getUserId());
        }catch (Exception e){
            log.error("退出登入异常",e);
            return fail();
        }
    }

}
