package com.sqx.modules.activityManage.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sqx.common.utils.Result;
import com.sqx.modules.activityManage.entity.ActivityManage;
import com.sqx.modules.activityManage.dao.ActivityManageDao;
import com.sqx.modules.activityManage.service.ActivityManageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.modules.app.entity.UserEntity;
import com.sqx.modules.app.service.UserService;
import com.sqx.modules.common.entity.CommonInfo;
import com.sqx.modules.common.service.CommonInfoService;
import com.sqx.modules.goods.dao.GoodsShopDao;
import com.sqx.modules.goods.entity.GoodsShop;
import com.sqx.modules.goods.service.GoodsShopService;
import com.sqx.modules.utils.HttpClientUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author www.javacoder.top
 * @since 2022-11-04
 */
@Service
public class ActivityManageServiceImpl extends ServiceImpl<ActivityManageDao, ActivityManage> implements ActivityManageService {
    @Autowired
    private ActivityManageDao activityManageDao;
    @Autowired
    private GoodsShopService goodsShopService;
    @Autowired
    private UserService userService;
    @Autowired
    private GoodsShopDao goodsShopDao;
    @Autowired
    private CommonInfoService commonInfoService;
    @Override
    public Result addActivity(ActivityManage activityManage) {
        activityManage.setCreateTime(new Date());
        activityManage.setUpdateTime(new Date());
        return Result.upStatus(activityManageDao.insert(activityManage));

    }

    @Override
    public Result updateActivity(ActivityManage activityManage) {
        if (activityManage.getActivityId() == null) {
            return Result.error("活动id不能为空");
        }
        return Result.upStatus(activityManageDao.updateById(activityManage));

    }

    @Override
    public Result deleteActivity(Long activityId) {

        return Result.upStatus(activityManageDao.deleteById(activityId));


    }

    @Override
    public IPage<ActivityManage> getActivityList(Integer page, Integer limit, ActivityManage activityManage) {
        IPage<ActivityManage> pages;
        if (page != null && limit != null) {
            pages = new Page<>(page, limit);
        } else {
            pages = new Page<>();
        }
        return activityManageDao.selectPage(pages, new QueryWrapper<>(activityManage));
    }

    @Override
    public ActivityManage getActivityInfo(Long activityId) {

        return activityManageDao.selectById(activityId);

    }

    @Override
    public IPage<GoodsShop> getShoActivityShopList(Integer page, Integer limit, Double lng, Double lat, GoodsShop goodsShop) {
        Page<GoodsShop> pages;
        if (page != null && limit != null) {
            pages = new Page<>(page, limit);
        } else {
            pages = new Page<>();
        }
        //如果没有经纬度，则默认为北京的经纬度
       if(lng==null && lat==null){
           lng = 121.47;
           lat = 31.23;
       }

       goodsShop.setShopLng(lng);
       goodsShop.setShopLat(lat);


        String way = commonInfoService.findOne(416).getValue();
        if("1".equals(way)){
            CommonInfo one = commonInfoService.findOne(235);
            String url = "https://apis.map.qq.com/ws/geocoder/v1/";
            Map<String, String> maps = new HashMap<>();
            maps.put("location", lat + "," + lng);
            maps.put("key", one.getValue());
            String data = HttpClientUtil.doGet(url, maps);
            JSONObject jsonObject = JSON.parseObject(data);
            String status = jsonObject.getString("status");
            if ("0".equals(status)) {
                JSONObject result = jsonObject.getJSONObject("result");
                JSONObject adInfo = result.getJSONObject("ad_info");
                String city = (String)adInfo.get("city");
                goodsShop.setCity(city);
                return goodsShopDao.getShoActivityShopList(pages, goodsShop);
            }
        }else{
            String value = commonInfoService.findOne(417).getValue();
            String url="http://api.tianditu.gov.cn/geocoder";
            Map<String,String> param=new HashMap<>();
            JSONObject postStr=new JSONObject();
            postStr.put("lon",lng);
            postStr.put("lat",lat);
            postStr.put("ver","1");
            param.put("postStr",postStr.toJSONString());
            param.put("type","geocode");
            param.put("tk",value);
            String s = HttpClientUtil.doGet(url,param);
            JSONObject jsonObject = JSONObject.parseObject(s);
            String status = jsonObject.getString("status");
            if("0".equals(status)){
                JSONObject result = jsonObject.getJSONObject("result");
                JSONObject addressComponent = result.getJSONObject("addressComponent");
                String city = addressComponent.getString("city");
                String province = addressComponent.getString("province");
                String county = addressComponent.getString("county");
                if(StringUtils.isEmpty(city)){
                    if("新疆维吾尔自治区".equals(province) || "台湾省".equals(province)){
                        city = addressComponent.getString("county");
                    }else{
                        city = addressComponent.getString("province");
                    }
                }
                goodsShop.setCity(city);
                return goodsShopDao.getShoActivityShopList(pages, goodsShop);
            }
        }
        return null;
    }


    @Override
    public IPage<GoodsShop> getAdminShoActivityShopList(Integer page, Integer limit, GoodsShop goodsShop) {
        Page<GoodsShop> pages;
        if (page != null && limit != null) {
            pages = new Page<>(page, limit);
        } else {
            pages = new Page<>();
        }
        return goodsShopDao.getAdminShoActivityShopList(pages, goodsShop);
    }




    @Override
    public Result cancelActivityShop(Long activityId, Long shopId) {
        return Result.upStatus(goodsShopService.cancelActivityShop(activityId, shopId));

    }

    @Override
    public Result shopJoinActivity(Long shopId, Long activityId, Integer auth) {
        ActivityManage activityManage = activityManageDao.selectById(activityId);
        if (activityManage == null) {
            return Result.error("当前活动不存在");
        }
        if (auth != 1) {
            if (activityManage.getIsAllowShop() == 0) {
                return Result.error("当前活动不允许商家自行加入");
            }
        }

        GoodsShop goodsShop = new GoodsShop();
        goodsShop.setShopId(shopId);
        goodsShop.setActivityId(activityId);
        goodsShopService.updateById(goodsShop);
        return Result.success();

    }

    @Override
    public Result addShopActivity(Long activityId, Long shopId) {
        UserEntity entity = new UserEntity();

        userService.updateById(entity);
        return Result.success();
    }
}
