package com.huigou.topsun.product.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.huigou.cache.DictUtil;
import com.huigou.topsun.base.coderule.domain.model.CodeRuleKind;
import com.huigou.topsun.product.domain.Product;
import com.huigou.topsun.product.application.ProductApplication;
import com.huigou.topsun.resource.vo.ResourceFiledVo;
import org.springframework.data.domain.Page;
import com.huigou.uasp.annotation.ControllerMapping;
import com.huigou.uasp.client.CommonController;
import com.huigou.util.SDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ProductController
 *
 * @author qinzhenguan
 * @date 2023/11/27 11:19
 **/
@Controller
@ControllerMapping("/product")
public class ProductController extends CommonController {

    @Autowired
    private ProductApplication productService;

    @Override
    protected String getPagePath() {
        return "/biz/topsun/product/";
    }

    /**
     * description 
     * @author qinzhenguan
     * @date 2023/11/27 11:49
     * @return null
     */
    public String findProduct() {//需要特定类型返回值
        SDO sdo = this.getSDO();
//        int page = sdo.getInteger("page");
//        int size = sdo.getInteger("size");
        ArrayList<Map<String,Object>> productPage = productService.findProductPage(0, 30);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("Rows",productPage);
        return toResult(map);
    }

    public String queryDetailAll() {
        SDO sdo = this.getSDO();
        String productId = sdo.getString("id");
        Map<String, Object> map = productService.queryDetailAll(Integer.parseInt(productId));

        return toResult(map);
    }

    public String forwardProduct() {
        // 通过此方法可以查看系统配置的 字典 的数据结构
        DictUtil.getDictionaryList("technologyType");

        Map<String, Object> map4 = new HashMap<>();
        map4.put("六六六", "666");
        map4.put("七七七", "777");
        map4.put("八八八", "888");
        this.putAttribute("selectedCategory", map4);
        return forward("product");
    }

    public String forwardProductDetail() {
        ObjectMapper mapper = new ObjectMapper();
        SDO sdo = this.getSDO();
        Map param = null;
        try {
            param = mapper.readValue(sdo.getString("data"), Map.class);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        Map<String, Object> map = productService.queryDetailAll(Integer.parseInt(param.get("productId") + ""));
        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.putAll(mapper.convertValue(map.get("detail"), Map.class));
        resultMap.putAll(param);
        //字典
        DictUtil.getDictionaryList("technologyType");
//        this.putAttribute("selectedCategory", map.get("selectedCategory"));

        Map<String, Object> map4 = new HashMap<>();
        map4.put("code", "666");
        map4.put("name", "777");
        map4.put("value", "888");
        this.putAttribute("selectedCategory", map4);


        return forward("productDetail", resultMap);
    }



}