package com.ningdatech.carapi.company.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ningdatech.carapi.company.entity.NdCompanyCreditDeduct;
import com.ningdatech.carapi.company.entity.dto.CompanyCreditDeductPageParam;
import com.ningdatech.carapi.company.service.INdCompanyCreditDeductService;
import com.ningdatech.carapi.sys.entity.dto.CompanyDTO;
import com.ningdatech.carapi.sys.entity.dto.RegionDTO;
import com.ningdatech.carapi.sys.helper.CompaniesCacheHelper;
import com.ningdatech.carapi.sys.helper.RegionsCacheHelper;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 平安办扣分 前端控制器
 * </p>
 *
 * @author liushuai
 * @since 2022-10-10
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/company-credit-deduct")
public class NdCompanyCreditDeductController {
    private final INdCompanyCreditDeductService iNdCompanyCreditDeductService;
    private final RegionsCacheHelper regionsCacheHelper;
    private final CompaniesCacheHelper companiesCacheHelper;

    @PostMapping
    private Boolean saveCreditDeduct(@RequestBody NdCompanyCreditDeduct ndCompanyCreditDeduct) {
        CompanyDTO company = companiesCacheHelper.getById(ndCompanyCreditDeduct.getCompanyId());
        if (0 != company.getRegionId() && null != company.getRegionId()){
            RegionDTO region = regionsCacheHelper.getByRegionId(company.getRegionId());
            ndCompanyCreditDeduct.setRegionName(region.getRegionName());
        }
        return iNdCompanyCreditDeductService.save(ndCompanyCreditDeduct);
    }

    @GetMapping
    private Page<NdCompanyCreditDeduct> getCreditDeductList(@ModelAttribute CompanyCreditDeductPageParam param) {
        return iNdCompanyCreditDeductService.getCreditDeductList(param);
    }

    @PutMapping
    private Boolean updateCreditDeduct(@RequestBody NdCompanyCreditDeduct ndCompanyCreditDeduct) {
        return iNdCompanyCreditDeductService.updateById(ndCompanyCreditDeduct);
    }

    @DeleteMapping
    private Boolean deleteCreditDeduct(@RequestBody NdCompanyCreditDeduct ndCompanyCreditDeduct) {
        return iNdCompanyCreditDeductService.delete(ndCompanyCreditDeduct.getId());
    }
}
