package ${packageName}.controller;

import com.zksy.common.core.controller.BaseController;
import com.zksy.system.basicData.domain.${ClassName};
import com.zksy.system.basicData.service.${ClassName}Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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 java.util.List;

/**
 * ${functionName}Controller
 *
 * @author ${author}
 * @date ${datetime}
 */
@RestController
@RequestMapping("/${className}")
@Api(tags = "${functionName}", description = "${functionName}desc")
public class ${ClassName}Controller extends BaseController{

    @Autowired
    private ${ClassName}Service service;

    @GetMapping("/getById/{borrowId}")
    @ApiOperation(value = "${functionName}搜索getById")
    public ${ClassName} getById(@PathVariable String borrowId) {
        return service.getById(borrowId);
    }

    @GetMapping("/findByPage")
    @ApiOperation(value = "${functionName}分页")
    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 = "${functionName}查询所有")
    public List<${ClassName}> getList(String conditionJson) throws Exception {
        return service.list(SearchUtil.parseWhereSql(conditionJson));
    }

    /**
     * 新增${functionName}
     */
    @PostMapping("/save")
    @ApiOperation(value = "${functionName}新增")
    @Log(title = "新增${functionName}", businessType = BusinessType.INSERT)
    public boolean save(@RequestBody ${ClassName} ${className}) {
        return service.save(${className});
    }

    /**
     * 修改${functionName}
     */
    @PostMapping("/updateById")
    @ApiOperation(value = "${functionName}修改")
    @Log(title = "修改${functionName}", businessType = BusinessType.UPDATE)
    public boolean updateById(@RequestBody ${ClassName} ${className}) {
        return service.updateById(${className});
    }

    /**
     * 导入${functionName}
     */
    @ApiOperation("${functionName}导入")
    @Log(title = "${functionName}", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file)
    {
        try {
            List<${ClassName}> ${className}All = service.list();
            List<${ClassName}> list = ExcelUtils.readMultipartFile(file, ${ClassName}.class,${ClassName}.class.getDeclaredFields().length - 2);
            if(${className}All != null && ${className}All.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("${functionName}导出")
    @Log(title = "${functionName}", businessType = BusinessType.EXPORT)
    @PostMapping("/exportData")
    public AjaxResult exportData(HttpServletResponse response,String conditionJson){
        try {
            List<${ClassName}> wrapperList = service.list(SearchUtil.parseWhereSql(conditionJson));
            ExcelUtils.export(response,"${functionName}",wrapperList,${ClassName}.class);
            return AjaxResult.success("导出数据成功");
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error("导出数据失败");
        }
    }

    @Log(title = "${functionName}", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    @ApiOperation(value = "删除${functionName}", notes = "删除${functionName}")
    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,"${functionName}模板");
    }
}

