package com.mortals.xhx.module.product.web;

import com.mortals.framework.common.Rest;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.web.BaseCRUDJsonBodyMappingController;
import com.mortals.xhx.base.system.param.service.ParamService;
import com.mortals.xhx.common.code.YesNoEnum;
import com.mortals.xhx.common.pdu.RespData;
import com.mortals.xhx.common.pdu.skin.SkinBasePdu;
import com.mortals.xhx.feign.skin.ISkinBaseFeign;
import com.mortals.xhx.module.platform.model.PlatformQuery;
import com.mortals.xhx.module.platform.service.PlatformService;
import com.mortals.xhx.module.product.model.ProductEntity;
import com.mortals.xhx.module.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 产品
 *
 * @author zxfei
 * @date 2022-06-27
 */
@RestController
@RequestMapping("product")
public class ProductController extends BaseCRUDJsonBodyMappingController<ProductService, ProductEntity, Long> {

    @Autowired
    private PlatformService platformService;
    @Autowired
    private ISkinBaseFeign skinBaseFeign;

    public ProductController() {
        super.setModuleDesc("产品");
    }

    @Override
    protected void init(Map<String, Object> model, Context context) {
        //获取所有产品皮肤 分组显示
        Rest<RespData<List<SkinBasePdu>>> resp = skinBaseFeign.list(new SkinBasePdu());
        if (resp.getCode() == YesNoEnum.YES.getValue()) {
            Map<String, Map<String, String>> skinProductCodeMap = resp.getData().getData().stream().collect(Collectors.groupingBy(x -> x.getProductCode(), Collectors.toMap(a -> a.getImageResolution(), b -> b.getImageResolutionValue(),(o,n)->n)));
            this.addDict(model, "skinProductCodeMap", skinProductCodeMap);
        }


        this.addDict(model, "platformId", platformService.find(new PlatformQuery()).stream().collect(Collectors.toMap(x -> x.getId().toString(), y -> y.getPlatformName())));
        super.init(model, context);
    }


}