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


import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.annotation.UnAuth;
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.Result;
import com.mortals.xhx.busiz.applets.req.CompanyReq;
import com.mortals.xhx.common.utils.BeanUtil;
import com.mortals.xhx.module.company.model.CompanyPatentEntity;
import com.mortals.xhx.module.company.model.CompanyPatentQuery;
import com.mortals.xhx.module.company.model.CompanyProductQuery;
import com.mortals.xhx.module.company.model.CompanyQuery;
import com.mortals.xhx.module.company.service.CompanyPatentService;
import com.mortals.xhx.module.company.service.CompanyProductService;
import com.mortals.xhx.module.company.service.CompanyService;
import com.mortals.xhx.module.labels.model.LabelsQuery;
import com.mortals.xhx.module.labels.service.LabelsService;
import com.mortals.xhx.module.product.model.ProductCategoryQuery;
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;


/**
 * @author ZYW
 * @date 2023-10-09 9:39
 */
@RestController
@Slf4j
@RequestMapping("/api/v1/company")
public class CompanyApiController extends AbstractBaseController<CompanyReq>{
    @Autowired
    private CompanyService companyService;
    @Autowired
    private LabelsService labelsService;
    @Autowired
    private CompanyPatentService companyPatentService;
    @Autowired
    private ProductService productService;
    @Autowired
    private CompanyProductService companyProductService;


    /**
     * 公司标注列表
     */
    @PostMapping(value = "labelsList")
    public Rest<Object> categoryList(){
        String busiDesc = "公司标注";
        Rest<Object> rest = Rest.ok();
        try {
            rest.setData(labelsService.find(new LabelsQuery()));
        }catch (Exception e) {
            log.error(busiDesc, e);
            rest = Rest.fail(super.convertException(e));
        }
        return rest;
    }

    /**
     * 公司列表
     */
    @PostMapping(value = "companyList")
    public Rest<Object> companyList(){
        String busiDesc = "公司列表";
        Rest<Object> rest = Rest.ok();
        try {
            rest.setData(companyService.find(new CompanyQuery()));
        }catch (Exception e) {
            log.error(busiDesc, e);
            rest = Rest.fail(super.convertException(e));
        }
        return rest;
    }

    /**
     * 公司知识产权数量列表及推荐产品列表
     */
    @PostMapping(value = "companyInfo")
    @UnAuth
    public Rest<Object> companyInfo(@RequestBody CompanyReq companyReq){
        String busiDesc = "公司详情";
        log.info("【{}】【请求体】--> {}", busiDesc, JSONObject.toJSONString(companyReq));
        Rest<Object> rest = Rest.ok(busiDesc + " 【成功】");
        try {
            if (ObjectUtils.isEmpty(companyReq.getCompanyId())) {
                throw new AppException("详细查询id不能为空!");
            }

            CompanyPatentQuery companyPatentQuery = new CompanyPatentQuery();
            companyPatentQuery.setCompanyId(companyReq.getCompanyId());

            CompanyProductQuery companyProductQuery = new CompanyProductQuery();
            companyProductQuery.setCompanyId(companyReq.getCompanyId());
            ProductEntity productEntity = new ProductEntity();
            productEntity.setCompanyId(String.valueOf(companyReq.getCompanyId()));


            JSONObject data = new JSONObject();
            data.put("patentList",companyPatentService.find(companyPatentQuery));
            data.put("productList",productService.getProductEntitiesByCompanyId(productEntity,companyProductService.find(companyProductQuery)));

            rest.setData(data);
            recordSysLog(request, busiDesc + " 【成功】");
        } catch (Exception e) {
            log.error(busiDesc, e);
            rest = Rest.fail(super.convertException(e));
        }
        return rest;
    }

}
