package com.ningdatech.carapi.company.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ningdatech.carapi.company.entity.NdCompanyQualityManage;
import com.ningdatech.carapi.company.entity.dto.ReqQualityManageDTO;
import com.ningdatech.carapi.company.entity.dto.ResQualityManageDTO;
import com.ningdatech.carapi.company.service.INdCompanyQualityManageService;
import com.ningdatech.carapi.sys.helper.RegionsCacheHelper;
import com.ningdatech.carapi.user.util.LoginUserUtil;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 质量通报 前端控制器
 * </p>
 *
 * @author liushuai
 * @since 2022-10-10
 */
@RestController
@RequestMapping("/api/v1/company-quality-manage")
@RequiredArgsConstructor
public class NdCompanyQualityManageController {

    private final INdCompanyQualityManageService iNdCompanyQualityManageService;
    private final RegionsCacheHelper regionsCacheHelper;

    @PostMapping
    public Boolean saveQualityManage(@RequestBody ReqQualityManageDTO reqQualityManageDTO) {
        NdCompanyQualityManage entity = new NdCompanyQualityManage();
        BeanUtil.copyProperties(reqQualityManageDTO, entity);
        entity.setDownloadCount(0);
        entity.setCompanyId(LoginUserUtil.loginUserDetail().getCompanyId());
        entity.setRegionName(regionsCacheHelper.getDisplayName(reqQualityManageDTO.getRegionId()));
        return iNdCompanyQualityManageService.save(entity);
    }

    @GetMapping
    public Page<ResQualityManageDTO> qualityManageList(@RequestParam(value = "regionId", required = false) Long regionId,
                                                        @RequestParam(value = "year", required = false) String year,
                                                        @RequestParam(value = "pageNumber") int pageNum,
                                                        @RequestParam(value = "pageSize") int pageSize) {
        return iNdCompanyQualityManageService.qualityManageList(regionId, year, pageNum, pageSize);
    }

    @DeleteMapping
    public Boolean deleteQualityManage(@RequestBody ReqQualityManageDTO reqQualityManageDTO) {
        return iNdCompanyQualityManageService.delete(reqQualityManageDTO.getId());
    }

    @ApiOperation("增加下载次数")
    @GetMapping("/incrementDownCount/{id}")
    public void incrementDownCount(@PathVariable Long id) {
        iNdCompanyQualityManageService.incrementDownCount(id);
    }
}
