package com.ruoyi.web.controller.api;

import cn.hutool.crypto.SmUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ApiResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.mall.domain.MallBanner;

import com.ruoyi.mall.domain.MallCompanyUser;
import com.ruoyi.mall.service.IMallBannerService;
import com.ruoyi.mall.service.IMallCompanyUserService;
import com.ruoyi.set.domain.SetRegion;
import com.ruoyi.set.service.ISetRegionService;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * @ClassName ApiIndexController
 * @Description 首页接口
 * @Author LCL
 * @Date 2024/03/11
 * @Version 1.0
 **/

@RestController
@RequestMapping("/api/index")
@ResponseBody
public class ApiIndexController {

    @Resource
    private IMallBannerService mallBannerService;
    @Resource
    private IMallCompanyUserService mallCompanyUserService;
    @Resource
    private ISetRegionService setRegionService;


    /**
     * 用户登陆
     *
     * @param params userAccount 用户账号
     *               userPassword 用户密码
     * @return mallCompanyUser
     */
    @PostMapping("/userLogin")
    @Log(title = "用户登陆", businessType = BusinessType.OTHER)
    public AjaxResult userLogin(@RequestBody Map<String, Object> params) {
        if (!params.containsKey("userAccount") || StringUtils.isBlank(params.get("userAccount").toString())) {
            return AjaxResult.error("缺少userAccount");
        }
        if (!params.containsKey("userPassword") || StringUtils.isBlank(params.get("userPassword").toString())) {
            return AjaxResult.error("缺少userPassword");
        }

        try {
            String userAccount = MapUtils.getString(params, "userAccount");
            String userPassword = MapUtils.getString(params, "userPassword");

            MallCompanyUser mallCompanyUser = mallCompanyUserService.selectMallCompanyUserByCompanyUserAccount(userAccount);
            if (ObjectUtils.isNotEmpty(mallCompanyUser)) {
                // 判断公司基站管理员状态
                // mallCompanyUserService.determineCompanyUser(mallCompanyUser.getCompanyUserId());

                if ("1".equals(mallCompanyUser.getCompanyUserStatus())) {
                    return AjaxResult.error("此账号已停用");
                }

                boolean matchesPassword = SecurityUtils.matchesPassword(userPassword, mallCompanyUser.getCompanyUserPassword());
                if (!matchesPassword) {
                    return AjaxResult.error("账号或密码错误");
                }
            } else {
                return AjaxResult.error("账号不存在");
            }
            return AjaxResult.success().put("data", mallCompanyUser);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("用户登陆异常");
        }
    }

    /**
     * 获取轮播图列表
     *
     * @return data
     */
    @GetMapping("/getBannerList")
    public AjaxResult getBannerList() {
        try {
            MallBanner mallBanner = new MallBanner();
            mallBanner.setStatus("0");
            List<MallBanner> mallBannerList = mallBannerService.selectMallBannerList(mallBanner);
            return AjaxResult.success().put("data", mallBannerList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取轮播图异常");
        }
    }

    /**
     * 获取省列表
     *
     * @return 行政区域集合
     */
    @GetMapping("/getProvinceList")
    public AjaxResult getProvinceList() {
        try {
            List<SetRegion> list = setRegionService.selectRegionProvinceList();
            return AjaxResult.success().put("data", list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取省列表异常");
        }
    }

    /**
     * 获取市或区县列表
     *
     * @param id 行政区域ID
     * @return 行政区域集合
     */
    @GetMapping("/getCityOrAreaList")
    public AjaxResult getCityOrAreaList(Integer id) {
        try {
            List<SetRegion> list = setRegionService.selectRegionCityOrAreaList(id);
            return AjaxResult.success().put("data", list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取市或区县列表异常");
        }
    }

}
