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.CrmAdministrativePenalty;
import com.zksy.system.basicData.service.CrmAdministrativePenaltyService;
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 lombok.extern.slf4j.Slf4j;
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-04-08
 */
@Slf4j
@RestController
@RequestMapping("/crmAdministrativePenalty")
@Api(tags = "行政处罚", description = "行政处罚desc")
public class CrmAdministrativePenaltyController extends BaseController {

    @Autowired
    private CrmAdministrativePenaltyService 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 CrmAdministrativePenalty crmAdministrativePenalty) {
        return service.save(crmAdministrativePenalty);
    }

    /**
     * 修改行政处罚
     */
    @PostMapping("/updateById")
    @ApiOperation(value = "行政处罚修改")
    @Log(title = "修改行政处罚", businessType = BusinessType.UPDATE)
    public boolean updateById(@RequestBody CrmAdministrativePenalty crmAdministrativePenalty) {
        return service.updateById(crmAdministrativePenalty);
    }

    /**
     * 导入行政处罚
     */
    @ApiOperation("行政处罚导入")
    @Log(title = "行政处罚", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file)
    {
        try {
            List<CrmAdministrativePenalty> crmAdministrativePenaltyAll = service.list();
            List<CrmAdministrativePenalty> list = ExcelUtils.readMultipartFile(file, CrmAdministrativePenalty.class,CrmAdministrativePenalty.class.getDeclaredFields().length-2);
            if(crmAdministrativePenaltyAll != null && crmAdministrativePenaltyAll.size() > 0){
                //return AjaxResult.error("数据已存在，如需重新导入请清空数据");
            }
            log.info("这是处理后的val的值:"+list);
            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<CrmAdministrativePenalty> wrapperList = service.list(SearchUtil.parseWhereSql(conditionJson));
            ExcelUtils.export(response,"行政处罚",wrapperList,CrmAdministrativePenalty.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));
    }

    @Log(title = "行政处罚", businessType = BusinessType.DELETE)
    @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,"行政处罚数据模板");
    }
}

