package com.ruoyi.benyun.controller;

import com.ruoyi.benyun.entity.vo.BrandEvolveVo;
import com.ruoyi.benyun.entity.vo.BrandOpcloseVo;
import com.ruoyi.benyun.entity.vo.ListQueryBody;
import com.ruoyi.benyun.service.BrandService;
import com.ruoyi.benyun.constant.RedisContant;
import com.ruoyi.benyun.entity.bo.BrandGeoLabelBo;
import com.ruoyi.benyun.entity.bo.BrandOpcloseBo;
import com.ruoyi.benyun.entity.bo.BrandSearch;
import com.ruoyi.benyun.utils.HashUtil;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/brand")
public class BrandController extends BaseController {
    @Autowired
    private BrandService brandService;
    @Autowired
    RedisTemplate redisTemplate;

//    获取品牌信息列表
    @GetMapping("/search")
    public R search(String text){
        List<BrandSearch> list = brandService.searchByLikeName(text);
        return R.ok(list);
    }

//    获取品牌信息列表
    @PostMapping("/list")
    public R list(ListQueryBody body){
        String userId = "1";
        return R.ok(brandService.searchListByMulti(body,userId));
    }

//    获取品牌详细信息
    @GetMapping("/info")
    public R info(String brandId){
        String userId = "1";
        return R.ok(brandService.searchByBrandId(brandId,userId));
    }

//    获取品牌物业信息
    @GetMapping("/property")
    public R property(String brandId){
        return R.ok(brandService.searchPropertyByBrandId(brandId));
    }

//    获取品牌城市分布词云
    @GetMapping("/distribution")
    public R distribution(String brandId){
        return R.ok(brandService.searchDisCloudByBrandId(brandId));
    }

//    获取相似品牌
    @GetMapping("/similar")
    public R similar(String brandId){
        return R.ok(brandService.searchSimilarByBrandId(brandId));
    }

//    获取品牌网点营业状态分布
    @GetMapping("/status")
    public R statusDistribution(String brandId){
        return R.ok(brandService.searchStatusByBrandId(brandId));
    }

//    获取品牌网点地图信息
    @GetMapping("/area")
    public R area(String brandId){
        return R.ok(brandService.searchProvinceByBrandId(brandId));
    }

//    获取品牌网点数量及其城市数量分布
    @GetMapping("/shopCount")
    public R shopCount(String brandId){
        return R.ok(brandService.searchDistributionByBrandId(brandId));
    }

//    品牌网点数量对比
    @GetMapping("/total")
    public R total(Integer pageSize){
        return R.ok(brandService.searchTotal(pageSize));
    }

//    获取品牌均分分布
//    已废弃
//    @GetMapping("/average")
//    public R average(String brandId){
//        return R.ok(brandService.searchAvgScoreByBrandId(brandId));
//    }

//    获取各地区品牌网点数量分布情况
//    已废弃
//    @PostMapping("/countDistribution")
//    public R categoryDis(ListQueryBody body){
//        List<String> addrCode = body.getAddrCode();
//        return R.ok(brandService.searchAddrDistribution(addrCode));
//    }

//    获取品牌地理位置标签分布
    @GetMapping("/geolabel")
    public R geolabel(String brandId){
        String md5 = HashUtil.hash(brandId,"MD5");
        List<BrandGeoLabelBo> bos = (List<BrandGeoLabelBo>) redisTemplate.boundHashOps(RedisContant.BRAND_GEOLABEL).get(md5);
        if (bos != null)
            return R.ok(bos);

        List<BrandGeoLabelBo> brandGeoLabelBos = brandService.searchGeoLabel(brandId);

        redisTemplate.boundHashOps(RedisContant.BRAND_GEOLABEL).put(md5,brandGeoLabelBos);
        redisTemplate.expire(RedisContant.BRAND_GEOLABEL, RedisContant.BRAND_GEOLABEL_TIME, TimeUnit.MINUTES);

        return R.ok(brandGeoLabelBos);
    }

//    行业连锁品牌发展情况
    @PostMapping("/evolve")
    public R develop(@RequestBody ListQueryBody body){
        List<String> addrCode = body.getAddrCode();
        List<String> typeCode = body.getTypeCode(); // 网点分类（犇云）
        List<String> brandId = body.getBrandId();
        if (brandId == null || brandId.size() == 0)
            return R.fail("brandId不允许为空");
        List<BrandEvolveVo> vos = brandService.searchEvolveByMulti(addrCode, typeCode, brandId);
        return R.ok(vos);
    }

//    连锁品牌近期开关店情况（开点量）
    @PostMapping("/open")
    public R open(@RequestBody BrandOpcloseBo bo){
        List<String> typeCodes = bo.getTypeCodes(); // 网点类型（犇云）
        LocalDateTime startTime = bo.getStartTime();
        LocalDateTime endTime = bo.getEndTime();
        List<BrandOpcloseVo> vos = brandService.searchOpenByMulti(typeCodes, startTime, endTime);
        return R.ok(vos);
    }

//    连锁品牌近期开关店情况（关店量）
    @PostMapping("/close")
    public R close(@RequestBody BrandOpcloseBo bo){
        List<String> typeCodes = bo.getTypeCodes(); // 网点类型（犇云）
        LocalDateTime startTime = bo.getStartTime();
        LocalDateTime endTime = bo.getEndTime();
        List<BrandOpcloseVo> vos = brandService.searchCloseByMulti(typeCodes, startTime, endTime);
        return R.ok(vos);
    }
}
