package com.zsElectric.boot.business.controller.applet;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zsElectric.boot.business.model.entity.Advertising;
import com.zsElectric.boot.business.model.entity.UserAccount;
import com.zsElectric.boot.business.model.entity.UserInfo;
import com.zsElectric.boot.business.model.entity.UserOrderInfo;
import com.zsElectric.boot.business.service.*;
import com.zsElectric.boot.charging.service.ChargingReceptionService;
import com.zsElectric.boot.common.constant.SystemConstants;
import com.zsElectric.boot.common.util.electric.RequestParmsEntity;
import com.zsElectric.boot.common.util.electric.ResponseParmsEntity;
import com.zsElectric.boot.core.web.Result;
import com.zsElectric.boot.system.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zsElectric.boot.business.model.query.StationInfoQuery;
import com.zsElectric.boot.business.model.vo.BannerInfoVO;
import com.zsElectric.boot.business.model.vo.StationInfoMapVO;
import com.zsElectric.boot.business.model.vo.StationInfoVO;
import com.zsElectric.boot.core.web.PageResult;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@Slf4j
@Tag(name = "小程序主页相关接口")
@RestController
@RequestMapping("/applet/v1/homePage")
@RequiredArgsConstructor
public class AppletHomeController {

    private final AppletHomeService appletHomeService;

    private final AdvertisingService advertisingService;

    private final ChargingReceptionService chargingReceptionService;

    /**
     * 用户端分页查询站点信息
     *
     * @param queryParams 查询参数
     * @return 站点信息分页列表
     */
    @Operation(summary = "用户端分页查询站点信息")
    @PostMapping("/getStationInfoPage")
    public PageResult<StationInfoVO> getStationInfoPage(@RequestBody StationInfoQuery queryParams) {
        IPage<StationInfoVO> result = appletHomeService.getStationInfoPage(queryParams);
        return PageResult.success(result);
    }

    /**
     * 首页地图模式-获取充电站列表（按距离排序）
     *
     * @param longitude 经度
     * @param latitude  纬度
     * @return 站点列表
     */
    @Operation(summary = "首页地图模式-获取充电站列表（按距离排序）")
    @GetMapping("/getStationInfoMapList")
    public Result<List<StationInfoMapVO>> getStationInfoMapList(
            @RequestParam BigDecimal longitude,
            @RequestParam BigDecimal latitude) {
        return Result.success(appletHomeService.getStationInfoMapList(longitude, latitude));
    }

    /**
     * 获取首页Banner列表
     *
     * @return Banner列表
     */
    @Operation(summary = "获取首页Banner列表")
    @GetMapping("/getBannerList/{location}")
    public Result<List<BannerInfoVO>> getBannerList(@PathVariable("location") Integer location) {
        return Result.success(appletHomeService.getBannerList(location));
    }

    /**
     * 获取首页广告弹窗列表
     *
     * @return 广告弹窗列表
     */
    @Operation(summary = "获取首页广告弹窗列表")
    @GetMapping("/getAdvertisingList")
    public Result<List<Advertising>> getAdvertisingList() {
        List<Advertising> list = advertisingService.list(Wrappers.lambdaQuery(Advertising.class)
                .eq(Advertising::getStatus, SystemConstants.STATUS_ONE)
                .eq(Advertising::getPosition, SystemConstants.STATUS_ONE)
                .orderByAsc(Advertising::getSort)
        );
        return Result.success(list);
    }

    @Operation(summary = "test")
    @PostMapping("/test")
    public ResponseParmsEntity test(@RequestBody RequestParmsEntity requestDTO) throws Exception {
        return chargingReceptionService.chargeOrderResponse(requestDTO);
    }

    @Autowired
    private UserInfoService userInfoService;

    @Operation(summary = "test2")
    @PostMapping("/test2")
    public void test2() throws Exception {
        // 调用恢复方法
        int restoredCount = userInfoService.restoreDeletedUsersByOrderAndAccount();
        log.info("恢复用户数量：{}", restoredCount);
    }

    @Operation(summary = "test3")
    @PostMapping("/test3")
    @Transactional(rollbackFor = Exception.class)
    public void test3(){
        List<UserInfo> list = userInfoService.list();

        for (UserInfo userInfo : list) {

            if (ObjectUtil.isEmpty(userInfo.getOpenid())) {
                continue;
            }
            String openid = userInfo.getOpenid();
            String result = openid.substring(openid.indexOf("oIE4l"));

            UserInfo one = userInfoService.getOne(Wrappers.lambdaQuery(UserInfo.class).eq(UserInfo::getOpenid, openid));
            if (one != null) {
                log.info("用户ID：{},用户openid:{},------{}", one.getId(), openid,one.getOpenid());
                continue;
            }

            userInfo.setOpenid(result);
            userInfoService.updateById(userInfo);
        }

    }
}
