package com.zksy.web.controller.basicData;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
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.common.utils.file.ExcelUtils;
import com.zksy.system.basicData.domain.CrmNewEnterpriseRecognitionDetails;
import com.zksy.system.basicData.service.CrmNewEnterpriseRecognitionDetailsService;
import com.zksy.utils.DowntemplateUtil;
import com.zksy.utils.MergeReadsExcelUtil;
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 org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * “专精特新”小巨人认定情况明细Controller
 *
 * @author sy
 * @date 2024-05-06
 */
@RestController
@RequestMapping("/crmNewEnterpriseRecognitionDetails")
@Api(tags = "“专精特新”小巨人认定情况明细", description = "“专精特新”小巨人认定情况明细desc")
public class CrmNewEnterpriseRecognitionDetailsController extends BaseController {

    @Autowired
    private CrmNewEnterpriseRecognitionDetailsService service;

    @GetMapping("/getById/{borrowId}")
    @ApiOperation(value = "“专精特新”小巨人认定情况明细搜索getById")
    public AjaxResult getById(@PathVariable String borrowId) {
        return AjaxResult.success(service.getById(borrowId));
    }

    @GetMapping("/findByPage")
    @ApiOperation(value = "“专精特新”小巨人认定情况明细分页")
    public Page findByPage(long pageNum, long pageSize, String conditionJson) throws Exception {
        return service.page(new Page<>(pageNum, pageSize), SearchUtil.parseWhereSql(conditionJson));
    }

    @GetMapping("/getList")
    @ApiOperation(value = "“专精特新”小巨人认定情况明细查询所有")
    public AjaxResult getList(String conditionJson) throws Exception {
        return AjaxResult.success(service.list(SearchUtil.parseWhereSql(conditionJson)));
    }

    /**
     * 新增“专精特新”小巨人认定情况明细
     */
    @PostMapping("/save")
    @ApiOperation(value = "“专精特新”小巨人认定情况明细新增")
    @Log(title = "新增“专精特新”小巨人认定情况明细", businessType = BusinessType.INSERT)
    public boolean save(@RequestBody CrmNewEnterpriseRecognitionDetails crmNewEnterpriseRecognitionDetails) {
        return service.save(crmNewEnterpriseRecognitionDetails);
    }

    /**
     * 修改“专精特新”小巨人认定情况明细
     */
    @PostMapping("/updateById")
    @ApiOperation(value = "“专精特新”小巨人认定情况明细修改")
    @Log(title = "修改“专精特新”小巨人认定情况明细", businessType = BusinessType.UPDATE)
    public boolean updateById(@RequestBody CrmNewEnterpriseRecognitionDetails crmNewEnterpriseRecognitionDetails) {
        return service.updateById(crmNewEnterpriseRecognitionDetails);
    }

    /**
     * 导入“专精特新”小巨人认定情况明细
     */
    @ApiOperation("“专精特新”小巨人认定情况明细导入")
    @Log(title = "“专精特新”小巨人认定情况明细", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file)
    {
        try {
            List<CrmNewEnterpriseRecognitionDetails> crmNewEnterpriseRecognitionDetailsAll = service.list();
            //List<CrmNewEnterpriseRecognitionDetails> list = ExcelUtils.readMultipartFile(file, CrmNewEnterpriseRecognitionDetails.class,CrmNewEnterpriseRecognitionDetails.class.getDeclaredFields().length - 2);
            if(crmNewEnterpriseRecognitionDetailsAll != null && crmNewEnterpriseRecognitionDetailsAll.size() > 0){
                //return AjaxResult.error("数据已存在，如需重新导入请清空数据");
            }
            //excel单元格合并导入
            //参数：文件， 表头列数， 是否读取序号0-读取、1-不读取， 表头占的行数下标从0开始的
            List<Map<String,String>> list = MergeReadsExcelUtil.readExcelToObj(file,CrmNewEnterpriseRecognitionDetails.class.getDeclaredFields().length - 2,1,1, CrmNewEnterpriseRecognitionDetails.class);
            List<CrmNewEnterpriseRecognitionDetails> crmNewEnterpriseRecognitionDetails= JSON.parseObject(JSON.toJSONString(list), new TypeReference<List<CrmNewEnterpriseRecognitionDetails>>() {});

            if(list.size() > 0){
                service.saveBatch(crmNewEnterpriseRecognitionDetails);
                return success("导入成功，共计："+list.size()+"条");
            }else{
                return AjaxResult.error("无数据");
            }
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error("导入数据失败");
        }
    }

    @ApiOperation("“专精特新”小巨人认定情况明细导出")
    @Log(title = "“专精特新”小巨人认定情况明细", businessType = BusinessType.EXPORT)
    @PostMapping("/exportData")
    public AjaxResult exportData(HttpServletResponse response, String conditionJson){
        try {
            List<CrmNewEnterpriseRecognitionDetails> wrapperList = service.list(SearchUtil.parseWhereSql(conditionJson));
            ExcelUtils.export(response,"“专精特新”小巨人认定情况明细",wrapperList,CrmNewEnterpriseRecognitionDetails.class);
            return AjaxResult.success("导出数据成功");
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error("导出数据失败");
        }
    }

    @Log(title = "“专精特新”小巨人认定情况明细", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    @ApiOperation(value = "删除“专精特新”小巨人认定情况明细", notes = "删除“专精特新”小巨人认定情况明细")
    public AjaxResult delete(@RequestBody List<String> ids)
    {
        return toAjax(service.removeByIds(ids));
    }

    @DeleteMapping("/deleteAll")
    @ApiOperation(value = "清除数据", notes = "清除数据")
    public AjaxResult deleteAll(){
        return service.deleteAll();
    }
    @PostMapping("/getUploadTemplate")
    @ApiOperation(value = "获取上传模板", notes = "获取上传模板")
    public void getUploadTemplate(HttpServletResponse response) throws IOException {
        DowntemplateUtil.downloadTemplate(response,"“专精特新”小巨人认定情况模板");
    }
}

