package com.zksy.web.controller.basicData;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zksy.common.annotation.Log;
import com.zksy.common.core.controller.BaseController;
import com.zksy.common.core.domain.AjaxResult;
import com.zksy.common.enums.BusinessType;
import com.zksy.system.basicData.domain.CrmFile;
import com.zksy.system.basicData.domain.CrmNewsRelease;
import com.zksy.system.basicData.service.CrmFileService;
import com.zksy.system.basicData.service.CrmNewsReleaseService;
import com.zksy.utils.SearchUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 新闻发布Controller
 *
 * @author sy
 * @date 2024-05-21
 */
@RestController
@RequestMapping("/crmNewsRelease")
@Api(tags = "新闻发布", description = "新闻发布desc")
public class CrmNewsReleaseController extends BaseController {

    @Autowired
    private CrmNewsReleaseService service;

    @Autowired
    private CrmFileService crmFileService;

    @GetMapping("/getById/{borrowId}")
    @ApiOperation(value = "新闻发布搜索getById")
    public AjaxResult getById(@PathVariable String borrowId) {
        return service.getByCrmNewsReleaseId(borrowId);
    }

    @GetMapping("/findByPage")
    @ApiOperation(value = "新闻发布分页")
    public Page findByPage(long pageNum, long pageSize, String conditionJson) throws Exception {
        Page<CrmNewsRelease> page = service.page(new Page<>(pageNum, pageSize), SearchUtil.parseWhereSql(conditionJson));
        for(CrmNewsRelease entity : page.getRecords()){
            List<CrmFile> crmFile = crmFileService.selectByFid(entity.getId());
            if(crmFile != null) {
                entity.setFiles(crmFile);
            }
        }
        return page;
    }

    @GetMapping("/getList")
    @ApiOperation(value = "新闻发布查询所有")
    public AjaxResult getList(String conditionJson) throws Exception {
        List<CrmNewsRelease> list = service.list(SearchUtil.parseWhereSql(conditionJson));
        for(CrmNewsRelease entity : list){
            List<CrmFile> crmFile = crmFileService.selectByFid(entity.getId());
            if(crmFile != null) {
                entity.setFiles(crmFile);
            }
        }
        return AjaxResult.success(list);
    }

    /**
     * 新增新闻发布
     */
    @PostMapping("/save")
    @ApiOperation(value = "新闻发布新增")
    @Log(title = "新增新闻发布", businessType = BusinessType.INSERT)
    public AjaxResult save(@RequestBody CrmNewsRelease crmNewsRelease) {
        return service.saveCrmNewsRelease(crmNewsRelease);
    }

    /**
     * 修改新闻发布
     */
    @PostMapping("/updateById")
    @ApiOperation(value = "新闻发布修改")
    @Log(title = "修改新闻发布", businessType = BusinessType.UPDATE)
    public AjaxResult updateById(@RequestBody CrmNewsRelease crmNewsRelease) {
        return service.updateCrmNewsRelease(crmNewsRelease);
    }

    @Log(title = "新闻发布", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    @ApiOperation(value = "删除新闻发布", notes = "删除新闻发布")
    public AjaxResult delete(@RequestBody List<String> ids)
    {
        return service.removeCrmNewsRelease(ids);
    }

}

