package com.ruoyi.web.controller.api;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.config.JiPushService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.mall.domain.MallAboutPrivacy;
import com.ruoyi.mall.domain.MallBanner;
import com.ruoyi.mall.domain.MallCompanyUser;
import com.ruoyi.mall.service.IMallAboutPrivacyService;
import com.ruoyi.mall.service.IMallBannerService;
import com.ruoyi.mall.service.IMallCompanyUserService;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.web.bind.annotation.*;

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

/**
 * @ClassName ApiUserController
 * @Description 我的相关接口
 * @Author LCL
 * @Date 2024/03/11
 * @Version 1.0
 **/

@RestController
@RequestMapping("/api/user")
@ResponseBody
public class ApiUserController {

    @Resource
    private IMallCompanyUserService mallCompanyUserService;
    @Resource
    private IMallAboutPrivacyService mallAboutPrivacyService;


    /**
     * 获取用户信息
     *
     * @param params userId 用户ID
     * @return mallCompanyUser
     */
    @GetMapping("/getUserInfo")
    public AjaxResult getUserInfo(@RequestParam Map<String, Object> params) {
        if (!params.containsKey("userId") || StringUtils.isBlank(params.get("userId").toString())) {
            return AjaxResult.error("缺少userId");
        }

        try {
            Long userId = MapUtils.getLong(params, "userId");

            // 判断公司基站管理员状态
            mallCompanyUserService.determineCompanyUser(userId);

            return AjaxResult.success().put("data", mallCompanyUserService.selectMallCompanyUserByCompanyUserId(userId));
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取用户信息异常");
        }
    }

    /**
     * 获取关于我们或隐私协议
     *
     * @param params type 类型：1-关于我们，2-隐私协议
     * @return data
     */
    @GetMapping("/getAboutUsOrPrivacy")
    public AjaxResult getAboutUsOrPrivacy(@RequestParam Map<String, Object> params) {
        if (!params.containsKey("type") || StringUtils.isBlank(params.get("type").toString())) {
            return AjaxResult.error("缺少type");
        }

        try {
            Long type = MapUtils.getLong(params, "type");

            MallAboutPrivacy mallAboutPrivacy = mallAboutPrivacyService.selectMallAboutPrivacyById(type);
            return AjaxResult.success().put("data", mallAboutPrivacy);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取关于我们或隐私协议异常");
        }
    }

    /**
     * 修改用户头像
     *
     * @param params userId 公司基站管理员ID
     *               companyUserHead 用户头像
     * @return mallCompanyUser
     */
    @RepeatSubmit
    @PostMapping("/updateCompanyUserHead")
    @Log(title = "修改用户头像", businessType = BusinessType.UPDATE)
    public AjaxResult updateCompanyUserHead(@RequestBody Map<String, Object> params) {
        if (!params.containsKey("userId") || StringUtils.isBlank(params.get("userId").toString())) {
            return AjaxResult.error("缺少userId");
        }
        if (!params.containsKey("companyUserHead") || StringUtils.isBlank(params.get("companyUserHead").toString())) {
            return AjaxResult.error("缺少companyUserHead");
        }

        try {
            Long userId = MapUtils.getLong(params, "userId");
            String companyUserHead = MapUtils.getString(params, "companyUserHead");

            MallCompanyUser mallCompanyUser = new MallCompanyUser();
            mallCompanyUser.setCompanyUserId(userId);
            mallCompanyUser.setCompanyUserHead(companyUserHead);
            mallCompanyUserService.updateMallCompanyUser(mallCompanyUser);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("修改用户头像异常");
        }
    }

    /**
     * 用户注销
     *
     * @param params userId 公司基站管理员ID
     * @return mallCompanyUser
     */
    @RepeatSubmit
    @PostMapping("/userLogOff")
    @Log(title = "用户注销", businessType = BusinessType.FORCE)
    public AjaxResult userLogOff(@RequestBody Map<String, Object> params) {
        if (!params.containsKey("userId") || StringUtils.isBlank(params.get("userId").toString())) {
            return AjaxResult.error("缺少userId");
        }

        try {
            Long userId = MapUtils.getLong(params, "userId");

            MallCompanyUser mallCompanyUser = new MallCompanyUser();
            mallCompanyUser.setCompanyUserId(userId);
            mallCompanyUser.setCompanyUserStatus("1");
            mallCompanyUserService.updateMallCompanyUser(mallCompanyUser);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("用户注销异常");
        }
    }

    /**
     * 极光推送
     *
     * @param params userId 用户ID
     * @return mallCompanyUser
     */
    @GetMapping("/someonePush")
    public AjaxResult someonePush(@RequestParam Map<String, Object> params) {
        if (!params.containsKey("userId") || StringUtils.isBlank(params.get("userId").toString())) {
            return AjaxResult.error("缺少userId");
        }

        try {
            Long userId = MapUtils.getLong(params, "userId");

            MallCompanyUser companyUser = mallCompanyUserService.selectMallCompanyUserByCompanyUserId(userId);
            List<String> memberIdList = new ArrayList<>();
            memberIdList.add(companyUser.getAuroraId());
            JiPushService.cabPush(memberIdList, "您有新的信息!", 4);

            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("极光推送异常");
        }
    }

}
