package com.zksy.web.controller.basicData;

import com.zksy.common.core.controller.BaseController;
import com.zksy.common.core.domain.AjaxResult;
import com.zksy.common.utils.file.ExcelUtils;
import com.zksy.system.basicData.domain.CrmEnergyElectricityPenalty;
import com.zksy.system.basicData.service.CrmEnergyElectricityPenaltyService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zksy.utils.DowntemplateUtil;
import com.zksy.utils.SearchUtil;
import com.zksy.common.annotation.Log;
import com.zksy.common.enums.BusinessType;
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;

/**
 * 能源电力-涉电领域行政处罚信息Controller
 *
 * @author sy
 * @date 2024-06-03
 */
@RestController
@RequestMapping("/crmEnergyElectricityPenalty")
@Api(tags = "能源电力-涉电领域行政处罚信息", description = "能源电力-涉电领域行政处罚信息desc")
public class CrmEnergyElectricityPenaltyController extends BaseController{

    @Autowired
    private CrmEnergyElectricityPenaltyService service;

    @GetMapping("/getById/{borrowId}")
    @ApiOperation(value = "能源电力-涉电领域行政处罚信息搜索getById")
    public CrmEnergyElectricityPenalty getById(@PathVariable String borrowId) {
        return 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 List<CrmEnergyElectricityPenalty> getList(String conditionJson) throws Exception {
        return service.list(SearchUtil.parseWhereSql(conditionJson));
    }

    /**
     * 新增能源电力-涉电领域行政处罚信息
     */
    @PostMapping("/save")
    @ApiOperation(value = "能源电力-涉电领域行政处罚信息新增")
    @Log(title = "新增能源电力-涉电领域行政处罚信息", businessType = BusinessType.INSERT)
    public boolean save(@RequestBody CrmEnergyElectricityPenalty crmEnergyElectricityPenalty) {
        return service.save(crmEnergyElectricityPenalty);
    }

    /**
     * 修改能源电力-涉电领域行政处罚信息
     */
    @PostMapping("/updateById")
    @ApiOperation(value = "能源电力-涉电领域行政处罚信息修改")
    @Log(title = "修改能源电力-涉电领域行政处罚信息", businessType = BusinessType.UPDATE)
    public boolean updateById(@RequestBody CrmEnergyElectricityPenalty crmEnergyElectricityPenalty) {
        return service.updateById(crmEnergyElectricityPenalty);
    }

    /**
     * 导入能源电力-涉电领域行政处罚信息
     */
    @ApiOperation("能源电力-涉电领域行政处罚信息导入")
    @Log(title = "能源电力-涉电领域行政处罚信息", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file)
    {
        try {
            List<CrmEnergyElectricityPenalty> crmEnergyElectricityPenaltyAll = service.list();
            List<CrmEnergyElectricityPenalty> list = ExcelUtils.readMultipartFile(file, CrmEnergyElectricityPenalty.class,CrmEnergyElectricityPenalty.class.getDeclaredFields().length - 2);
            if(crmEnergyElectricityPenaltyAll != null && crmEnergyElectricityPenaltyAll.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 AjaxResult exportData(HttpServletResponse response, String conditionJson){
        try {
            List<CrmEnergyElectricityPenalty> wrapperList = service.list(SearchUtil.parseWhereSql(conditionJson));
            ExcelUtils.export(response,"能源电力-涉电领域行政处罚信息",wrapperList,CrmEnergyElectricityPenalty.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,"能源电力-涉电领域行政处罚信息模板");
    }
}

