package com.fdkankan.ucenter.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fdkankan.common.exception.BusinessException;
import com.fdkankan.ucenter.common.Result;
import com.fdkankan.ucenter.constant.LoginConstant;
import com.fdkankan.ucenter.entity.HotShop;
import com.fdkankan.ucenter.service.IHotShopService;
import com.fdkankan.ucenter.vo.RequestHotShop;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 热点与商品关联表 前端控制器
 * </p>
 *
 * @author 
 * @since 2022-11-02
 */
@RestController
@RequestMapping("/ucenter/hot/shop")
public class HotShopController {

    @Autowired
    IHotShopService hotShopService;


    /**
     * 热点id找小程序商品id
     */
    @RequestMapping(value = "/findGoodIdByHotId")
    public Result<List<HotShop>> findGoodIdByHotId(@RequestBody RequestHotShop param) throws Exception {
        if(StringUtils.isEmpty(param.getHotId()) || StringUtils.isEmpty(param.getOrigin())){
            throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
        }
        LambdaQueryWrapper<HotShop> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(HotShop::getHotId,param.getHotId());
        wrapper.eq(HotShop::getOrigin,param.getOrigin());
        if(StringUtils.isNotBlank(param.getPageSid())){
            wrapper.eq(HotShop::getPageSid,param.getPageSid());
        }
        return Result.success(hotShopService.list(wrapper));
    }

    /**
     * 热点id找小程序商品id
     */
    @RequestMapping(value = "/findGoodIdByHotIdList")
    public Result<List<HotShop>> findGoodIdByHotIdList(@RequestBody RequestHotShop param) throws Exception {
        if(StringUtils.isEmpty(param.getHotId()) || StringUtils.isEmpty(param.getOrigin())){
            throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
        }
        LambdaQueryWrapper<HotShop> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(HotShop::getHotId,param.getHotId());
        wrapper.eq(HotShop::getOrigin,param.getOrigin());
        return Result.success(hotShopService.list(wrapper));
    }

    /**
     * 保存热点与商品关联
     */
    @RequestMapping(value = "/saveHotShop")
    public Result saveHotShop(@RequestBody RequestHotShop param) throws Exception {
        if(StringUtils.isEmpty(param.getHotId()) && StringUtils.isEmpty(param.getOrigin())){
            throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
        }

        String[] pageSids = null;
        if(StringUtils.isNotEmpty(param.getPageSid())){
            pageSids = param.getPageSid().split(",");
        }
        HotShop hotShopEntity = null;

        //查看是否有已经关联的商品，有则删除
        LambdaQueryWrapper<HotShop> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(HotShop::getHotId,param.getHotId());
        wrapper.eq(HotShop::getOrigin,param.getOrigin());
        hotShopService.remove(wrapper);


        if(StringUtils.isNotEmpty(param.getGoodsIds())){
            String[] ids = param.getGoodsIds().split(",");
            for(int i = 0, len = ids.length; i < len; i ++){
                hotShopEntity = new HotShop();
                hotShopEntity.setHotId(param.getHotId());
                hotShopEntity.setSceneNum(param.getSceneNum());
                hotShopEntity.setPageSid("0");
                if(pageSids != null){
                    hotShopEntity.setPageSid(pageSids[i]);
                }
                hotShopEntity.setGoodId(Long.valueOf(ids[i]));
                hotShopEntity.setOrigin(param.getOrigin());
                hotShopService.save(hotShopEntity);
            }
        }
        return Result.success();
    }

    /**
     * 删除热点商品关联
     */
    @RequestMapping(value = "/deleteGoodsHot")
    public Result deleteGoodsHot(@RequestBody RequestHotShop param) throws Exception {
        if(StringUtils.isEmpty(param.getHotId()) && StringUtils.isEmpty(param.getOrigin())){
            throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
        }
        LambdaQueryWrapper<HotShop> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(HotShop::getHotId,param.getHotId());
        wrapper.eq(HotShop::getOrigin,param.getOrigin());
        if(StringUtils.isNotBlank(param.getPageSid())){
            wrapper.eq(HotShop::getPageSid,param.getPageSid());
        }
        List<HotShop> list = hotShopService.list(wrapper);
        if(list != null && list.size() == 1){
            hotShopService.removeById(list.get(0).getId());
        }
        return Result.success();
    }
}

