package ${packageName}.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.gov.customs.cacp.sdks.core.result.Result;
import cn.gov.customs.cacp.sdks.core.user.trans.CacpTransUser;
import cn.gov.customs.cacp.sdks.core.user.trans.UserContextHolder;
import cn.gov.customs.wxjy.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import cn.gov.customs.wxjy.common.core.controller.BaseController;
import org.springframework.web.multipart.MultipartFile;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import ${packageName}.pojo.${ClassName};
import ${packageName}.pojo.${ClassName}Query;
import ${packageName}.service.I${ClassName}Service;
import cn.gov.customs.wxjy.common.utils.poi.ExcelUtil;
#if($table.crud || $table.sub)
import cn.gov.customs.wxjy.common.core.page.TableDataInfo;
#elseif($table.tree)
#end

/**
 * ${functionName}Controller
 *
 * @author ${author}
 * @date ${datetime}
 */
@RestController
@RequestMapping("/${moduleName}/${businessName}")
@RequiredArgsConstructor
public class ${ClassName}Controller extends BaseController {

    private final I${ClassName}Service ${className}Service;

    /**
     * 查询${functionName}列表
     */
    @PostMapping("/get-list")
    #if($table.crud || $table.sub)
    public Result<PageInfo<${ClassName}>> list(@RequestBody ${ClassName}Query query) {
        PageHelper.startPage(query.getPageIndex(), query.getPageSize());
        PageInfo<${ClassName}> list = ${className}Service.selectPageList(query);
        return Result.success(list);
    }
    #elseif($table.tree)
        public Result<PageInfo<${ClassName}>> list(${ClassName}Query query) {
            PageInfo<${ClassName}> list = ${className}Service.selectList(${className});
            return Result.success(list);
        }
    #end

    /**
     * 获取${functionName}详细信息
     */
    @GetMapping(value = "/get-detail")
    public Result<${ClassName}> getInfo(@RequestParam ${pkColumn.javaType} ${pkColumn.javaField}) {
        return Result.success(${className}Service.selectBy${pkColumn.capJavaField}(${pkColumn.javaField}));
    }

    /**
     * 新增${functionName}
     */
    @PostMapping("/insert-${className}")
    public Result<Integer> add(@RequestBody ${ClassName} ${className}) {
        return Result.success(${className}Service.insert(${className}));
    }

    /**
     * 修改${functionName}
     */
    @PostMapping("/update-${className}")
    public Result<Integer> edit(@RequestBody ${ClassName} ${className}) {
##        ${className}.setUpdateBy(getUserId().toString());
        return Result.success(${className}Service.update(${className}));
    }

    /**
     * 删除${functionName}
     */
    @PostMapping("/delete-${className}")
    public Result<Integer> remove(@RequestBody ${pkColumn.javaType}[] ids) {
        return Result.success(${className}Service.deleteBy${pkColumn.capJavaField}s(ids));
    }

    /**
    * 导入 ${functionName}
    * @param file
    * @param updateSupport
    * @return
    * @throws Exception
    */
    @PostMapping("/import-${className}")
    public Result<String> importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        CacpTransUser user = UserContextHolder.currentUser();
        ExcelUtil<${ClassName}> util = new ExcelUtil<${ClassName}>(${ClassName}.class);
        List<${ClassName}> ${className}List = util.importExcel(file.getInputStream());
        String message = ${className}Service.import${ClassName}(${className}List, updateSupport, user);
        return Result.success(message);
    }

    /**
     * 导出${functionName}列表
     */
    @PostMapping("/get-${className}-export")
    public void export(HttpServletResponse response, @RequestBody ${ClassName}Query query) {
        List<${ClassName}> list = ${className}Service.selectList(query);
        ExcelUtil<${ClassName}> util = new ExcelUtil<${ClassName}>(${ClassName}. class);
        util.exportExcel(response, list, "${functionName}数据");
    }

    /**
    * 获取导入模板
    * @param response
    */
    @PostMapping("/template-${className}")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<${ClassName}> util = new ExcelUtil<${ClassName}>(${ClassName}.class);
        util.importTemplateExcel(response, "${functionName}");
    }
}
