package com.ningdatech.carapi.company.controller;

import javax.validation.Valid;

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

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ningdatech.carapi.company.entity.NdCompanyCreditInside;
import com.ningdatech.carapi.company.entity.dto.CompanyCreditInsidePageParam;
import com.ningdatech.carapi.company.service.INdCompanyCreditInsideService;
import com.ningdatech.carapi.sys.entity.dto.CompanyDTO;
import com.ningdatech.carapi.sys.helper.CompaniesCacheHelper;
import com.ningdatech.carapi.sys.helper.RegionsCacheHelper;

import lombok.RequiredArgsConstructor;

/**
 * <p>
 * 内部/联合检查 前端控制器
 * </p>
 *
 * @author liushuai
 * @since 2022-10-10
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/company-credit-inside")
@Valid
public class NdCompanyCreditInsideController {
    private final INdCompanyCreditInsideService iNdCompanyCreditInsideService;
    private final RegionsCacheHelper regionsCacheHelper;

    private final CompaniesCacheHelper companiesCacheHelper;

    @PostMapping
    private Boolean saveCreditInside(@RequestBody NdCompanyCreditInside ndCompanyCreditInside) {
        CompanyDTO company = companiesCacheHelper.getById(ndCompanyCreditInside.getCompanyId());
        if (0 != company.getRegionId() && null != company.getRegionId()){
            ndCompanyCreditInside.setRegionName(regionsCacheHelper.getDisplayName(company.getRegionId()));
        }
        return iNdCompanyCreditInsideService.save(ndCompanyCreditInside);
    }

    @GetMapping
    private Page<NdCompanyCreditInside> getCreditInsideList(@Valid @ModelAttribute CompanyCreditInsidePageParam param) {
        return iNdCompanyCreditInsideService.getList(param);
    }

    @PutMapping
    private Boolean updateCreditInside(@RequestBody NdCompanyCreditInside ndCompanyCreditInside) {
        return iNdCompanyCreditInsideService.updateById(ndCompanyCreditInside);
    }

    @DeleteMapping
    private Boolean deleteCreditInside(@RequestBody NdCompanyCreditInside ndCompanyCreditInside) {
        return iNdCompanyCreditInsideService.delete(ndCompanyCreditInside.getId());
    }
}
