package ${packageName}.controller;

import com.starry.common.enums.BusinessType;
import com.starry.common.result.R;


import com.starry.common.annotation.Log;
import org.springframework.security.access.prepost.PreAuthorize;

import javax.annotation.Resource;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * ${functionName}Controller
 *
 * @author ${author}
 * @since  ${datetime}
 */
@RestController
@RequestMapping("/${moduleName}/${businessName}")
public class ${ClassName}Controller {
    @Resource
    private I${ClassName}Service ${className}Service;

/**
 * 查询${functionName}列表
 */
@PreAuthorize("@customSs.hasPermission('${permissionPrefix}:list')")
@GetMapping("/list")
    #if($table.crud || $table.sub)
    public R list(${ClassName}Entity ${className}) {
        IPage<${ClassName}Entity> list = ${className}Service.select${ClassName}ByPage(${className});
        return R.ok(list);
    }
    #elseif($table.tree)
        public R list(${ClassName}Entity ${className}) {
            List<${ClassName}Entity> list = ${className}Service.select${ClassName}List(${className});
            return R.ok(list);
        }
    #end

    /**
     * 获取${functionName}详细信息
     */
    @PreAuthorize("@customSs.hasPermission('${permissionPrefix}:query')")
    @GetMapping(value = "/{${pkColumn.javaField}}")
    public R getInfo(@PathVariable("${pkColumn.javaField}") ${pkColumn.javaType} ${pkColumn.javaField}) {
        return R.ok(${className}Service.select${ClassName}By${pkColumn.capJavaField}(${pkColumn.javaField}));
    }

    /**
     * 新增${functionName}
     */
    @PreAuthorize("@customSs.hasPermission('${permissionPrefix}:create')")
    @Log(title = "${functionName}", businessType = BusinessType.INSERT)
    @PostMapping("/create")
    public R create(@RequestBody ${ClassName}Entity ${className}) {
        boolean success = ${className}Service.create(${className});
        if (success) {
            return R.ok();
        }
        return R.error("添加失败");
    }

    /**
     * 修改${functionName}
     */
    @PreAuthorize("@customSs.hasPermission('${permissionPrefix}:edit')")
    @Log(title = "${functionName}", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/update/{${pkColumn.javaField}}")
    public R update(@PathVariable ${pkColumn.javaType} ${pkColumn.javaField}, @RequestBody ${ClassName}Entity ${className}) {
        ${className}.setId(${pkColumn.javaField});
        boolean success = ${className}Service.update(${className});
        if (success) {
            return R.ok();
        }
        return R.error("修改失败");
    }

    /**
     * 删除${functionName}
     */
    @PreAuthorize("@customSs.hasPermission('${permissionPrefix}:remove')")
    @Log(title = "${functionName}", businessType = BusinessType.DELETE)
    @DeleteMapping("/{${pkColumn.javaField}s}")
    public R remove(@PathVariable ${pkColumn.javaType}[] ${pkColumn.javaField}s) {
        return R.ok(${className}Service.delete${ClassName}By${pkColumn.capJavaField}s(${pkColumn.javaField}s));
    }
}
