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

import cn.hutool.core.util.IdUtil;
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.device.model.DeviceEntity;
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 lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
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")
@Slf4j
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() == null ? "" : a.getImageResolution(), b -> b.getImageResolutionValue() == null ? "" : 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);
    }

    /**
     * 产品升级版本
     */
    @PostMapping(value = "upGrade")
    public Rest<Void> upGrade(@RequestBody ProductEntity productEntity) {
        log.info("产品升级版本：{}", productEntity.getProductName());
        String busiDesc = this.getModuleDesc() + "升级版本";
        Rest<Void> rest = Rest.ok(busiDesc + " 【成功】");
        try {
            this.service.upGrade(productEntity.getId(), getContext());
            recordSysLog(request, busiDesc + " 【成功】");
        } catch (Exception e) {
            log.error("产品升级版本", e);
            rest = Rest.fail(super.convertException(e));
        }
        return rest;
    }


    public static void main(String[] args) {
        System.out.println(IdUtil.objectId());
        System.out.println(IdUtil.fastSimpleUUID());

    }


}