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.OwSelfReporting;
import com.zksy.system.basicData.domain.CrmFile;
import com.zksy.system.basicData.domain.dto.OwSelfReportingDTO;
import com.zksy.system.basicData.service.OwSelfReportingService;
import com.zksy.system.basicData.service.CrmFileService;
import com.zksy.utils.DowntemplateUtil;
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 javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 官网Controller
 *
 * @author sy
 * @date 2024-06-12
 */
@RestController
@RequestMapping("/owSelfReporting")
@Api(tags = "官网自主上报", description = "官网自主上报desc")
public class OwSelfReportingController extends BaseController {

    @Autowired
    private OwSelfReportingService service;

    @Autowired
    private CrmFileService crmFileService;

    @GetMapping("/getById/{borrowId}")
    @ApiOperation(value = "官网自主上报搜索getById")
    public AjaxResult getById(@PathVariable String borrowId) {
        return AjaxResult.success(service.getByOwSelfReportingId(borrowId));
    }


    @GetMapping("/findByPage")
    @ApiOperation(value = "官网上报分页")
    public Page<OwSelfReporting> findByPage(long pageNum, long pageSize, String conditionJson) throws Exception {
        Page<OwSelfReporting> page = service.page(new Page<>(pageNum, pageSize), SearchUtil.parseWhereSql(conditionJson));
        for (OwSelfReporting 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<OwSelfReporting> list = service.list(SearchUtil.parseWhereSql(conditionJson));
        for (OwSelfReporting 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 saveOwSelfReporting(@RequestBody OwSelfReportingDTO dto) {
            return service.saveOwSelfReporting(dto);
        }

        /**
         * 修改官网自主上报
         */
        @PostMapping("/updateById")
        @ApiOperation(value = "官网自主上报修改")
        @Log(title = "修改官网自主上报", businessType = BusinessType.UPDATE)
        public AjaxResult updateById(@RequestBody OwSelfReporting owSelfReporting) {
            return service.updateOwSelfReporting(owSelfReporting);
        }

        @Log(title = "删除官网自主上报", businessType = BusinessType.DELETE)
        @PostMapping("/delete")
        @ApiOperation(value = "删除官网自主上报", notes = "删除官网自主上报")
        public AjaxResult delete(@RequestBody List<String> ids) {
            return service.removeOwSelfReporting(ids);
        }

    @GetMapping("/downloadTemplateWord")
    @ApiOperation(value = "下载自主上报文档", notes = "下载自主上报文档")
    public void getUploadTemplate(HttpServletResponse response) throws IOException {
        DowntemplateUtil.downloadTemplate(response,"怀化高新区企业信用综合评价材料清单");
    }

    @GetMapping("/checkInformationByUnicode")
    @ApiOperation(value = "官网自主上报信息检查", notes = "官网自主上报信息检查")
    public AjaxResult checkInformationByUnicode(String unicode) {
        return service.checkInformationByUnicode(unicode);
    }
}

