package com.kiisoo.ic.webappmy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kiisoo.ic.common.BaseController;
import com.kiisoo.ic.employee.entity.PrivilageCpUserDO;
import com.kiisoo.ic.employee.mapper.PrivilageCpUserDOMapper;
import com.kiisoo.ic.login.bean.LoginBean;
import com.kiisoo.ic.store.constant.Constants;
import com.kiisoo.ic.store.entity.PoiStoreStaff;
import com.kiisoo.ic.store.entity.StoreUserDTO;
import com.kiisoo.ic.store.mapper.PoiStoreStaffDOMapper;
import com.kiisoo.ic.store.mapper.PrivilageCpUserStoreDOMapper;
import com.kiisoo.ic.store.service.StoreEmployeeService;
import com.kiisoo.ic.system.entity.PrivilageAccountDO;
import com.kiisoo.ic.system.entity.PrivilageRoleDO;
import com.kiisoo.ic.system.entity.PrivilageUserDO;
import com.kiisoo.ic.system.mapper.PrivilageRoleDOMapper;
import com.kiisoo.ic.system.mapper.PrivilageUserDOMapper;
import com.kiisoo.ic.system.service.PrivilageAccountService;
import com.kiisoo.ic.webappmy.service.WebAppService;
import com.kiisoo.ic.webappmy.vo.StafferInfoVO;
import com.kiisoo.ic.webappmy.vo.UserParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.kiisoo.ic.store.constant.Constants.BSD_REQ_QRCODE_TYPE_SELLER;
import static com.kiisoo.ic.store.constant.Constants.BSD_REQ_QRCODE_TYPE_STORE;

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

    @Autowired
    private PoiStoreStaffDOMapper poiStoreStaffDOMapper;

    @Autowired
    private PrivilageCpUserStoreDOMapper privilageCpUserStoreDOMapper;

    @Autowired
    private PrivilageCpUserDOMapper privilageCpUserDOMapper;

    @Autowired
    private StoreEmployeeService storeEmployeeService;

    @Autowired
    private WebAppService webAppService;

    /**
     * 用户 mapper
     */
    @Autowired
    private PrivilageUserDOMapper privilageUserDOMapper;

    /**
     * 角色 mapper
     */
    @Autowired
    private PrivilageRoleDOMapper privilageRoleDOMapper;

    /**
     * 账号service
     */
    @Autowired
    private PrivilageAccountService privilageAccountService;

    /**
     * 获取用户信息
     */
    @GetMapping("/get/staffer/info")
    public Map<String,Object> userInfo(long userId){
        try {
            List<StafferInfoVO> info = poiStoreStaffDOMapper.selectInfoById(userId);
            if(info.size() > 0){
                PrivilageUserDO privilageUserDO = privilageUserDOMapper.selectById(userId);

                // 用户不存在
                if(null == privilageUserDO){
                    return fail(com.kiisoo.ic.constants.Constants.ERROR_USER_NO_PERMISSION);
                }

                PrivilageAccountDO result = privilageAccountService.getAccountByUserId(userId);

                // 该账号已暂停，请联系店长
                if(null != result && result.getStatus().equals(2)){
                    return fail(com.kiisoo.ic.constants.Constants.ERROR_ACCOUNT_STOP);
                }

                PrivilageRoleDO roleDO = privilageRoleDOMapper.selectRoleName(userId);
                Long storeId = privilageUserDOMapper.selectStoreIdByUserId(userId);
                info.get(0).setRoleCode(roleDO.getCode());
                info.get(0).setStoreId(storeId);
                info.get(0).setUserId(userId);
                return data(info.get(0));
            }else {
                // 用户不存在
                return fail("0009");
            }
        }catch (Exception e){
            log.error("获取用户信息异常",e);
            return fail();
        }
    }

    /**
     * 获取门店推广码
     */
    @GetMapping("/get/shop/qr")
    public Map<String,Object> shopIcQr(long shopId){
        try {
            String img = null;
            List<Long> shopIds = new ArrayList<>();
            shopIds.add(shopId);
            List<StoreUserDTO> cpUserStoreDO = privilageCpUserStoreDOMapper.selectCpUserByShops(shopIds);
            if(cpUserStoreDO!=null && cpUserStoreDO.size() > 0){
                img = cpUserStoreDO.get(0).getQrCodeAction();
                if (StringUtils.isBlank(img)){
                    img = storeEmployeeService.getQrCodeFromBSD(cpUserStoreDO.get(0).getCpUserId(),BSD_REQ_QRCODE_TYPE_STORE,"0",cpUserStoreDO.get(0).getCode());
                    PrivilageCpUserDO update = new PrivilageCpUserDO();
                    update.setId(cpUserStoreDO.get(0).getUserId());
                    update.setQrCode(img);
                    privilageCpUserDOMapper.updateById(update);
                }
                return data(img);
            }else{
                return fail();
            }
        }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();
                if (StringUtils.isBlank(img)){
                    Long storeId = poiStoreStaffs.get(0).getStoreId();
                    String cpUserId = privilageCpUserStoreDOMapper.selectCpUserIdByStoreId(storeId);
                    img = storeEmployeeService.getQrCodeFromBSD(cpUserId,BSD_REQ_QRCODE_TYPE_SELLER,poiStoreStaffs.get(0).getStaffCode(),poiStoreStaffs.get(0).getStoreCode());
                    PoiStoreStaff update = new PoiStoreStaff();
                    update.setId(poiStoreStaffs.get(0).getId());
                    update.setEpWechatQrCode(img);
                    poiStoreStaffDOMapper.updateById(update);
                }
                return data(img);
            }else{
                return fail();
            }
        }catch (Exception e){
            log.error("获取用户推广码异常",e);
            return fail();
        }
    }

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

}
