package com.mortals.xhx.busiz.applets.web;


import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.common.Rest;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.model.OrderCol;
import com.mortals.framework.model.PageInfo;
import com.mortals.framework.model.Result;
import com.mortals.xhx.busiz.applets.req.ProductReq;
import com.mortals.xhx.common.utils.BeanUtil;
import com.mortals.xhx.module.category.model.CategoryQuery;
import com.mortals.xhx.module.category.service.CategoryService;
import com.mortals.xhx.module.product.model.ProductEntity;
import com.mortals.xhx.module.product.model.ProductQuery;
import com.mortals.xhx.module.product.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
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.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.mortals.framework.ap.SysConstains.PAGEINFO_KEY;
import static com.mortals.xhx.common.key.ErrorCode.ERROR_TOKEN_EXPIRED;
import static com.mortals.xhx.common.key.ErrorCode.ERROR_TOKEN_EXPIRED_CONTENT;


/**
 * @author ZYW
 * @date 2023-10-09 9:39
 */
@RestController
@Slf4j
@RequestMapping("/api/v1/product")
public class ProductApiController extends AbstractBaseController<ProductReq>{
    @Autowired
    private ProductService productService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 产品类型
     */
    @PostMapping(value = "categoryList")
    public Rest<Object> categoryList(){
        String busiDesc = "产品类型";
        Rest<Object> rest = Rest.ok();
        try {
            rest.setData(categoryService.find(new CategoryQuery()));
        }catch (Exception e) {
            log.error(busiDesc, e);
            rest = Rest.fail(super.convertException(e));
        }
        return rest;
    }

    /**
     * 产品列表
     */
    @PostMapping(value = "productList")
    public Rest<Object> productList(@RequestBody ProductReq productReq){
        String busiDesc = "产品列表列表";
        log.info("【{}】【请求体】--> {}", busiDesc, JSONObject.toJSONString(productReq));
        Rest<Object> rest = Rest.ok();
        Map<String, Object> model = new HashMap<>();
        Context context = this.getContext();
        if (ObjectUtils.isEmpty(context) || ObjectUtils.isEmpty(context.getUser())) {
            throw new AppException(ERROR_TOKEN_EXPIRED, ERROR_TOKEN_EXPIRED_CONTENT);
        }
        try {
            PageInfo pageInfo = buildPageInfo(productReq);
            ProductQuery query = new ProductQuery();

            if(!ObjectUtils.isEmpty(productReq.getCategoryId())){
                query.setCategoryId(String.valueOf(productReq.getCategoryId()));
            }
            query.setHot(productReq.getHot());
            query.setOrderColList(Arrays.asList(new OrderCol("createTime", OrderCol.DESCENDING)));
            Result<ProductEntity> result = productService.find(query, pageInfo, context);
            List<ProductEntity> collect = result.getList().stream().map(item -> {
                ProductEntity productEntity = new ProductEntity();
                BeanUtils.copyProperties(item, productEntity, BeanUtil.getNullPropertyNames(item));
                productEntity.setProductDetail(null);
                return productEntity;
            }).collect(Collectors.toList());
            model.put(KEY_RESULT_DATA, collect);
            model.put(PAGEINFO_KEY, result.getPageInfo());
            super.parsePageInfo(model, result.getPageInfo());
            rest.setData(model);
            recordSysLog(request, busiDesc + " 【成功】");
        } catch (Exception e) {
            log.error(busiDesc, e);
            rest = Rest.fail(super.convertException(e));
        }
        return rest;
    }

}
