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.common.utils.file.ExcelUtils;
import com.zksy.system.basicData.domain.CrmAdministrativeLicense;
import com.zksy.system.basicData.service.CrmAdministrativeLicenseService;
import com.zksy.utils.DowntemplateUtil;
import com.zksy.utils.SearchUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.DateUtil;
import org.aspectj.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * @author Administrator
 * @version 1.0
 * @project credit-rating
 * @description 行政许可
 * @date 2024/3/26 14:20:34
 */
@Api(value = "行政许可", tags = "行政许可")
@RestController
@RequestMapping("/crmAdministrativeLicense")
public class CrmAdministrativeLicenseController extends BaseController {
    @Autowired
    private CrmAdministrativeLicenseService service;

    @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)));
    }

    @ApiOperation("行政许可数据导入")
    @Log(title = "行政许可", businessType = BusinessType.IMPORT)
    //@PreAuthorize("@ss.hasPermi('system:user:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file)
    {
        try {
            List<CrmAdministrativeLicense> crmAdministrativeLicenseAll = service.list();
            List<CrmAdministrativeLicense> list = ExcelUtils.readMultipartFile(file, CrmAdministrativeLicense.class,CrmAdministrativeLicense.class.getDeclaredFields().length - 2);
            if(crmAdministrativeLicenseAll != null && crmAdministrativeLicenseAll.size() > 0) {
                //return AjaxResult.error("数据已存在，如需重新导入请清空数据");
            }
            if(list.size() > 0){
                service.saveBatch(list);
                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 void exportData(HttpServletResponse response,String conditionJson) throws Exception {
        List<CrmAdministrativeLicense> wrapperList = service.list(SearchUtil.parseWhereSql(conditionJson));
        ExcelUtils.export(response,"行政许可",wrapperList,CrmAdministrativeLicense.class);
    }

    @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,"行政许可数据模板");
    }
}

