package com.cftech.productclassify.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cftech.core.sql.Sort;
import com.cftech.productclassify.model.ProductMenuVO;
import com.cftech.productclassify.model.Productclassify;
import com.cftech.productclassify.dao.ProductclassifyMapper;
import com.cftech.productclassify.model.ProductclassifyVO;
import com.cftech.productclassify.service.ProductclassifyService;
import com.cftech.core.generic.GenericDao;
import com.cftech.core.generic.GenericServiceImpl;
import com.cftech.core.sql.Conds;
import com.esotericsoftware.minlog.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

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

/**
 * 产品分类ServiceImpl
 *
 * @author Buyj
 * @date: 2020-09-22 18:06
 */
@Service("productclassifyService")
public class ProductclassifyServiceImpl extends GenericServiceImpl<Productclassify> implements ProductclassifyService {

    @Autowired
    @Qualifier("productclassifyMapper")
    private ProductclassifyMapper productclassifyMapper;

    @Override
    public GenericDao<Productclassify> getGenericMapper() {
        return productclassifyMapper;
    }

    @Override
    public List<Productclassify> fetchTreeByPage(Conds conds, Sort sort, int pageNo, int pageSize) {
        Map<String, Object> params = new HashMap<>();
        params.put("conds", conds);
        params.put("offset", pageNo > 0 ? pageNo : 0);
        params.put("limit", pageSize > 0 ? pageSize : 0);
        params.put("sort", sort);
        return productclassifyMapper.fetchTreeByPage(params);
    }

    @Override
    public JSONObject productList(String productName,  String classifyName, String dosagaFrom) {
        JSONObject rtnJson = new JSONObject();
        try {
            List<ProductclassifyVO> productVOS = productclassifyMapper.productList(productName, classifyName, dosagaFrom);
            if (productVOS==null){
                rtnJson.put("errorNO","1");
                rtnJson.put("errorNo","查询失败");
            }
            Log.info("返回结果:"+productVOS);
            rtnJson.put("errorNO","0");
            rtnJson.put("data",productVOS);

        }catch (Exception e){
            e.printStackTrace();
            rtnJson.put("errorNO","1");
        }


        return rtnJson;

    }

    @Override
    public JSONObject productMenu() {
        JSONObject rtnJson = new JSONObject();
        try {
            List<ProductMenuVO> menu = productclassifyMapper.productMenu();
            if (menu==null){
                rtnJson.put("errorNO","1");
                rtnJson.put("errorMsg","查询失败");
            }
            Log.info("返回结果:"+menu);
            rtnJson.put("errorNO","0");
            rtnJson.put("data",menu);

        }catch (Exception e){
           e.printStackTrace();
            rtnJson.put("errorNO","1");
        }
        return rtnJson;
    }

}