package com.cyksj.web.controller.manage.goods;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.cyksj.common.annotation.Log;
import com.cyksj.common.exception.BusinessRuntimeException;
import com.cyksj.common.util.Jsons;
import com.cyksj.dto.Result;
import com.cyksj.enums.BusinessType;
import com.cyksj.enums.GatewayResponse;
import com.cyksj.model.entity.GoodsDon;
import com.cyksj.model.entity.GoodsDonCategory;
import com.cyksj.model.entity.GoodsDonSku;
import com.cyksj.model.manage.request.ReqGoodsSpuInsert;
import com.cyksj.model.manage.views.GoodsDonViewer;
import com.cyksj.model.views.GoodsDonCategoryDetailView;
import com.cyksj.model.views.UpgradePackageView;
import com.cyksj.service.mange.CmsGoodsDonService;
import com.cyksj.service.mange.GoodsDonCategoryService;
import com.cyksj.service.mange.RefreshCacheService;
import com.ejlchina.searcher.BeanSearcher;
import com.ejlchina.searcher.SearchResult;
import com.ejlchina.searcher.param.Operator;
import com.ejlchina.searcher.util.MapBuilder;
import com.ejlchina.searcher.util.MapUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author chan
 * @date 2022/9/27 4:45 PM
 */

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/manage/goods")
public class CmsGoodsDonController {

    private final HttpServletRequest request;

    private final BeanSearcher beanSearcher;

    private final CmsGoodsDonService goodsDonService;

    private final RefreshCacheService refreshCacheService;

    private final GoodsDonCategoryService goodsDonCategoryService;

    @GetMapping("/get")
    public Result<SearchResult<GoodsDonCategoryDetailView>> get(){
        SearchResult<GoodsDonCategoryDetailView> search = beanSearcher.search(GoodsDonCategoryDetailView.class, MapUtils.flatBuilder(request.getParameterMap())
                .field(GoodsDonCategoryDetailView::getDeleted,true)
		        .put("condition", "(special_type is null or special_type != 'giftCard')")
		        .build());
        return GatewayResponse.SUCCESS.newBuilder().toResult(search);
    }

    /**
     * 商品详情
     */
    @GetMapping("/get/{id}")
    public Result<GoodsDonViewer> getById(@PathVariable Long id) throws Exception {
        GoodsDonViewer goodsDonViewer = beanSearcher.searchFirst(GoodsDonViewer.class, MapUtils.builder().field(GoodsDonViewer::getId, id).build());
        if (StrUtil.isNotBlank(goodsDonViewer.getRecommendGoods())) {
            goodsDonViewer.setRecommendGoodsList(Jsons.parseList(goodsDonViewer.getRecommendGoods(), Long.class));
        }
        List<GoodsDonCategory> categoryView = goodsDonCategoryService.getCateGoryViewByGoodsId(id);
        goodsDonViewer.setCategoryList(categoryView.stream().map(GoodsDonCategory::getId).collect(Collectors.toList()));
        return GatewayResponse.SUCCESS.newBuilder().toResult(goodsDonViewer);
    }

    /**
     * 商品规格详情
     */
    @GetMapping("/get/sku/{id}")
    public Result<List<GoodsDonSku>> getSkuById(@PathVariable Long id, HttpServletRequest request) {
        List<GoodsDonSku> goodsDonSkus = beanSearcher.searchAll(GoodsDonSku.class, MapUtils.flatBuilder(request.getParameterMap()).field(GoodsDonSku::getGoodsId, id)
                .orderBy(GoodsDonSku::getSorted).asc()
                .build());
        return GatewayResponse.SUCCESS.newBuilder().toResult(goodsDonSkus);
    }

    /**
     * 新增 商品spu
     */
    @PostMapping(value = "/post")
    @SaCheckPermission("platform:add")
    @Log(module = "平台管理/新增平台", businessType = BusinessType.POST, isSaveRequestData = true)
    public Result<Long> spuInsert(@Validated @RequestBody ReqGoodsSpuInsert spu) throws Exception {
        // 新增商品
        GoodsDon goods = new GoodsDon();
        if (spu.getSecondType() == null) {
            spu.setSecondType(2);
        }
        BeanUtil.copyProperties(spu, goods);
        if (StrUtil.isNotEmpty(goods.getRecommendTitle())) {
            if (CollUtil.isEmpty(spu.getRecommendGoodsList())) {
                throw BusinessRuntimeException.getInstance("请选择推荐的对应平台");
            }
        }
        goodsDonService.save(goods);
        //保存平台 分类的 关联
        List<Long> categoryList = spu.getCategoryList();
        if (CollUtil.isNotEmpty(categoryList)) {
            goodsDonCategoryService.saveBatchRelate(goods.getId(), categoryList);
        }
        refreshCacheService.refreshYhHomePageCache();
        return GatewayResponse.SUCCESS.newBuilder().toResult(goods.getId());
    }

    /**
     * 修改 商品spu
     */
    @PutMapping(value = "/update")
    @SaCheckPermission("platform:update")
    @Log(module = "平台管理/编辑平台", businessType = BusinessType.PUT, isSaveRequestData = true)
    public Result<String> spuUpdate(@Validated @RequestBody ReqGoodsSpuInsert spu) throws Exception{
        GoodsDon goods = new GoodsDon();
        BeanUtil.copyProperties(spu,goods);
        if (spu.getSpecialType() == null) {
            goods.setSpecialType(null);
        }
        if (StrUtil.isNotEmpty(goods.getRecommendTitle())) {
            if (CollUtil.isEmpty(spu.getRecommendGoodsList())) {
                throw BusinessRuntimeException.getInstance("请选择推荐的对应平台");
            }
        }
        if (CollUtil.isNotEmpty(spu.getRecommendGoodsList())) {
            Set<Long> re_goodsIds = new HashSet<>(spu.getRecommendGoodsList());
            //去除自己
            re_goodsIds.remove(spu.getId());
            goods.setRecommendGoods(Jsons.toJson(re_goodsIds));
        }
        if (StrUtil.isEmpty(goods.getRecommendTitle())) {
            goods.setRecommendTitle(null);
        }
        if (StrUtil.isEmpty(goods.getVideo())) {
            goods.setVideo(null);
        }
        if (CollUtil.isNotEmpty(spu.getRecommendGoodsList())) {
            Set<Long> re_goodsIds = new HashSet<>(spu.getRecommendGoodsList());
            //去除自己
            re_goodsIds.remove(spu.getId());
            goods.setRecommendGoods(Jsons.toJson(re_goodsIds));
        }
        //平台 分类关系
        goodsDonCategoryService.handleGoodsRelate(goods.getId(), spu.getCategoryList());
        goodsDonService.updateById(goods);
        refreshCacheService.refreshYhHomePageCache();
        return GatewayResponse.SUCCESS.newBuilder().toResult();
    }


    @PutMapping("/put/status/{id}")
    @SaCheckPermission("platform:updateStatus")
    @Log(module = "平台管理/平台上下架", businessType = BusinessType.PUT, isSaveRequestData = true, isSaveResponseData = true)
    public Result<Boolean> updateStatus(@PathVariable Long id) {
        GoodsDon goods = goodsDonService.getById(id);
        if (goods == null) {
            throw BusinessRuntimeException.getInstance("平台不存在");
        }
        goods.setStatus(!goods.getStatus());
        goodsDonService.updateById(goods);
        refreshCacheService.refreshYhHomePageCache();
        return GatewayResponse.SUCCESS.newBuilder().toResult(goods.getStatus());
    }

    @DeleteMapping("/delete/{id}")
    @SaCheckPermission("platform:delete")
    @Log(module = "平台管理/删除平台", businessType = BusinessType.DELETE, isSaveRequestData = true, isSaveResponseData = true)
    public Result<Long> deleteById(@PathVariable Long id) {
        GoodsDon goods = goodsDonService.getById(id);
        if (goods == null) {
            throw BusinessRuntimeException.getInstance("平台不存在");
        }
        goods.setDeleted(false);
        goodsDonService.updateById(goods);
        refreshCacheService.refreshYhHomePageCache();
        return GatewayResponse.SUCCESS.newBuilder().toResult(id);
    }

    /**
     * 获取所有平台列表
     */
    @GetMapping("/get/all")
    public Result<List<GoodsDon>> getAllGoodsList(Boolean isNoBuyout) {
        MapBuilder mapBuilder = MapUtils.flatBuilder(request.getParameterMap());
        //过滤买断制 平台
        String conditionSql;
        if (isNoBuyout != null && isNoBuyout) {
            mapBuilder.field(GoodsDon::getSpecialType).op(Operator.IsNull);
            conditionSql = "(select count(*) from goods_don_sku sku where sku.goods_id = g.id and sku.months = 300) = 0";
        } else {
            conditionSql = "(special_type is null or special_type != 'giftCard')";
        }
        conditionSql += "  and exists (select id from goods_don_sku sku where sku.goods_id = g.id limit 1)";
        List<GoodsDon> goodsDons = beanSearcher.searchAll(GoodsDon.class, mapBuilder
                .put("condition", conditionSql)
                .field(GoodsDon::getDeleted, true)
                .onlySelect(GoodsDon::getId, GoodsDon::getCategory, GoodsDon::getType, GoodsDon::getTitle, GoodsDon::getSecondType).build());
        return GatewayResponse.SUCCESS.newBuilder().toResult(goodsDons);
    }

    /**
     * 获取礼品卡平台
     */
    @GetMapping("/get/giftCard")
    public Result<GoodsDon> getGiftCard() {
        GoodsDon goodsDon = beanSearcher.searchFirst(GoodsDon.class, MapUtils.builder()
                .field(GoodsDon::getSpecialType, GoodsDon.SpecialType.giftCard.name())
                .build());
        List<GoodsDonCategory> categoryView = goodsDonCategoryService.getCateGoryViewByGoodsId(goodsDon.getId());
        goodsDon.setCategoryList(categoryView.stream().map(GoodsDonCategory::getId).collect(Collectors.toList()));
        return GatewayResponse.SUCCESS.newBuilder().toResult(goodsDon);
    }

}
