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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.mortals.framework.common.Rest;
import com.mortals.framework.exception.AppException;
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.module.company.model.CompanyPatentEntity;
import com.mortals.xhx.module.company.model.CompanyPatentQuery;
import com.mortals.xhx.module.company.model.vo.HomeStatInfo;
import com.mortals.xhx.module.company.service.CompanyLabelsService;
import com.mortals.xhx.module.company.service.CompanyPatentService;
import com.mortals.xhx.module.labels.model.LabelsQuery;
import com.mortals.xhx.module.labels.service.LabelsService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.converter.json.JsonbHttpMessageConverter;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import com.mortals.framework.model.Context;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mortals.framework.web.BaseCRUDJsonBodyMappingController;
import com.mortals.xhx.module.company.model.CompanyEntity;
import com.mortals.xhx.module.company.service.CompanyService;
import org.apache.commons.lang3.ArrayUtils;
import com.mortals.framework.util.StringUtils;

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

import com.alibaba.fastjson.JSONObject;

import java.util.Arrays;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import static com.mortals.framework.ap.SysConstains.*;

/**
 * 公司
 *
 * @author zxfei
 * @date 2023-09-18
 */
@RestController
@RequestMapping("company")
public class CompanyController extends BaseCRUDJsonBodyMappingController<CompanyService, CompanyEntity, Long> {

    @Autowired
    private ParamService paramService;

    @Autowired
    private LabelsService labelsService;
    @Autowired
    private CompanyPatentService companyPatentService;


    public CompanyController() {
        super.setModuleDesc("公司");
    }

    @Override
    protected void init(Map<String, Object> model, Context context) {
        this.addDict(model, "totalEmployees", paramService.getParamBySecondOrganize("Company", "totalEmployees"));
        this.addDict(model, "softNum", paramService.getParamBySecondOrganize("Company", "softNum"));
        this.addDict(model, "patentNum", paramService.getParamBySecondOrganize("Company", "patentNum"));

        this.addDict(model, "labels", labelsService.find(new LabelsQuery()).stream().collect(Collectors.toMap(x -> x.getId().toString(), y -> y.getLabelName(), (o, n) -> n)));


        super.init(model, context);
    }

    @Override
    protected int editAfter(Long id, Map<String, Object> model, CompanyEntity entity, Context context) throws AppException {

        List<CompanyPatentEntity> companyPatentEntities = companyPatentService.find(new CompanyPatentQuery().companyId(entity.getId()));
        entity.setCompanyPatentsList(companyPatentEntities);


        return super.editAfter(id, model, entity, context);
    }

    /**
     * 首页统计
     */
    @PostMapping(value = "stat")
    public String indexStat() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put(KEY_RESULT_CODE, VALUE_RESULT_SUCCESS);
        jsonObject.put(KEY_RESULT_MSG, "");
        try {
            Rest<HomeStatInfo> rest = this.service.indexStat(getContext());
            if (YesNoEnum.YES.getValue() == rest.getCode()) {
                jsonObject.put(KEY_RESULT_DATA, rest.getData());
            }
            recordSysLog(request, "首页统计 【成功】");
        } catch (Exception e) {
            log.error("异常", e);
            jsonObject.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
            jsonObject.put(KEY_RESULT_MSG, super.convertException(e));
        }

        return JSON.toJSONString(jsonObject, SerializerFeature.DisableCircularReferenceDetect);

    }


}