#set($dashedName = $table.name.replace("_", "-"))
package ${package.Controller};

import com.foreverwin.modular.core.util.R;
import com.foreverwin.modular.core.util.FrontPage;
import com.foreverwin.modular.core.util.CommonMethods;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
#if(${superControllerClassPackage})
import ${superControllerClassPackage};
#end
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import ${package.Service}.${table.serviceName};
import ${package.Entity}.${entity};
#foreach($field in ${table.fields})
#if(${field.capitalName} == "CreateTime" || ${field.capitalName} == "UpdateTime")
import java.time.LocalDateTime;
#break;
#end
#end
import java.util.List;

/**
 *
 * @author ${author}
 * @since ${date}
 */
@RestController
@RequestMapping("#if(${package.ModuleName})/${package.ModuleName}#end/#if(${controllerMappingHyphenStyle})${controllerMappingHyphen}#else${dashedName}#end")
#if(${superControllerClass})
public class ${table.controllerName} extends ${superControllerClass} {
#else
public class ${table.controllerName} {
#end

    @Autowired
    public ${table.serviceName} ${table.entityPath}Service;

    /**
    * 根据id查询
    *
    * @param id 主键
    * @return
    */
    @ResponseBody
    @GetMapping("/{id:.+}")
    public R get${entity}ById(@PathVariable String id) {
        return R.ok( ${table.entityPath}Service.getById(id));
    }

    /**
     * 查询所有数据
     *
     * @return
     */
    @ResponseBody
    @GetMapping("")
    public R get${entity}List(${entity} ${table.entityPath}){
        List<${entity}> result;
        QueryWrapper<${entity}> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(${table.entityPath});
        result = ${table.entityPath}Service.list(queryWrapper);
        return R.ok(result);
    }

    /**
     * 分页查询数据
     *
     * @param frontPage  分页信息
     * @return
     */
    @ResponseBody
    @GetMapping("/page")
    public R page(FrontPage<${entity}> frontPage, ${entity} ${table.entityPath}){
        IPage result;
        QueryWrapper<${entity}> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(${table.entityPath});
        if (frontPage.getGlobalQuery() != null && !"".equals(frontPage.getGlobalQuery().trim())) {
            //TODO modify global query
            queryWrapper.lambda().and(wrapper -> wrapper
#foreach($field in ${table.fields})
#if(${field.propertyType.equals("String")})
#if($!{velocityCount} == 1)
                .like(${entity}::get${field.capitalName}, frontPage.getGlobalQuery())
#else
                .or().like(${entity}::get${field.capitalName}, frontPage.getGlobalQuery())
#end
#end
#end
    );
        }
        result = ${table.entityPath}Service.page(frontPage.getPagePlus(), queryWrapper);
        return R.ok(result);
    }

    /**
     * 新增
     * @param ${table.entityPath}  传递的实体
     * @return  null 失败  实体成功
     */
    @PostMapping
    public R save(@RequestBody ${entity} ${table.entityPath}) {
#foreach($field in ${table.fields})
#if(${field.capitalName} == "Creator" || ${field.capitalName} == "Updater")
        String user = CommonMethods.getUser();
#break;
#end
#end
#foreach($field in ${table.fields})
#if(${field.capitalName} == "CreateTime" || ${field.capitalName} == "UpdateTime")
        LocalDateTime now = LocalDateTime.now();
#break;
#end
#end
#foreach($field in ${table.fields})
#if(${field.capitalName} == "Creator")
        ${table.entityPath}.setCreator(user);
#end
#if(${field.capitalName} == "Updater")
        ${table.entityPath}.setUpdater(user);
#end
#end
#foreach($field in ${table.fields})
#if(${field.capitalName} == "CreateTime")
        ${table.entityPath}.setCreateTime(now);
#end
#if(${field.capitalName} == "UpdateTime")
        ${table.entityPath}.setUpdateTime(now);
#end
#end
        return R.ok(${table.entityPath}Service.save(${table.entityPath}));
    }

    /**
     * 修改
     * @param ${table.entityPath}  传递的实体
     * @return  null 失败  实体成功
     */
    @PutMapping
    public R updateById(@RequestBody ${entity} ${table.entityPath}) {
#foreach($field in ${table.fields})
#if(${field.capitalName} == "Updater")
		String user = CommonMethods.getUser();
        ${table.entityPath}.setUpdater(user);
#end
#end
#foreach($field in ${table.fields})
#if(${field.capitalName} == "UpdateTime")
		LocalDateTime now = LocalDateTime.now();
        ${table.entityPath}.setUpdateTime(now);
#end
#end
        return R.ok(${table.entityPath}Service.updateById(${table.entityPath}));
    }

    /**
     * 根据id删除对象
     * @param id  实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.DELETE, value = "/{id:.+}")
    public R removeById(@PathVariable("id") String id){
        return R.ok(${table.entityPath}Service.removeById(id));
    }

    /**
     * 批量删除对象
     * @param ids 实体集合ID
     * @return  0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/delete-batch")
    public R removeByIds(List<String> ids){
        return R.ok(${table.entityPath}Service.removeByIds(ids));
    }
}