package com.chinaweal.aiccs.crgs.entbase.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chinaweal.aiccs.common.base.controller.BaseController;
import com.chinaweal.aiccs.common.constant.Constant;
import com.chinaweal.aiccs.common.util.StringUtils;
import com.chinaweal.aiccs.crgs.entbase.entity.TCompanyDetails;
import com.chinaweal.aiccs.crgs.entbase.service.TCompanyDetailsService;
import com.chinaweal.youfool.framework.springboot.mybatis.plus.page.PageRequestDto;
import com.chinaweal.youfool.framework.springboot.rest.RestResult;
import com.chinaweal.youfool.framework.springboot.util.AssertUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
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 javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 企业信用风险画像 前端控制器
 * </p>
 *
 * @author <a href="https://blog.lroyia.top">lroyia</a>
 * @since 2021-02-01
 */
@Api("企业信息相关接口")
@RestController
@RequestMapping("/company")
public class TCompanyDetailsController extends BaseController {

    @Autowired
    private TCompanyDetailsService tCompanyDetailsService;

    /**
     * 通用分页
     * @param pageRequestDto 分页参数
     * @param request 请求信息
     * @return 查询结果
     * @author lroyia
     * @since 2021年2月1日 17:52:21
     */
    @ApiOperation("企业信息通用分页")
    @PostMapping("common_paging")
    public RestResult<?> getDefaultPage(@RequestBody PageRequestDto pageRequestDto, HttpServletRequest request){
        return RestResult.ok(tCompanyDetailsService.defaultPaging(pageRequestDto, wrapper -> {
            Map<String, Object> customParamMap = pageRequestDto.getCustomParamMap();
            if(customParamMap == null){
                return;
            }
            // 搜索统一码和注册号
            String searchUscc = StringUtils.tranObject(customParamMap.get("searchUscc"));
            if(StringUtils.isNotBlank(searchUscc)){
                wrapper.lambda().and(w -> w.like(TCompanyDetails::getUscc, searchUscc).or().like(TCompanyDetails::getRegNo, searchUscc));
            }
            // 搜索名称
            String searchName = StringUtils.tranObject(customParamMap.get("searchName"));
            if(StringUtils.isNotBlank(searchName)){
                wrapper.lambda().like(TCompanyDetails::getName, searchName);
            }
        }, null));
    }

    /**
     * 通过pripid查询企业信息
     * @param pripids 企业pripid(多个用半角逗号隔开)
     * @return 查询结果
     * @author lroyia
     * @since 2021年2月7日 16:04:37
     */
    @ApiOperation("通过pripid查询企业信息")
    @PostMapping("list_by_pripid")
    public RestResult<List<TCompanyDetails>> listByPripids(@ApiParam("企业pripid(多个用半角逗号隔开)") String pripids){
        AssertUtils.isNotBlank(pripids);
        return RestResult.ok(tCompanyDetailsService.list(new LambdaQueryWrapper<TCompanyDetails>().in(TCompanyDetails::getPripid, pripids.split(Constant.COMMA))));
    }
}

