package ${package}.${pkgName}.controller;

import java.util.Map;

import io.swagger.annotations.Api;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
#if(${checkAuth})
import org.springframework.security.access.prepost.PreAuthorize;
#end
import org.shoulder.core.dto.response.PageResult;
import org.shoulder.core.dto.response.BaseResult;

import ${package}.${pkgName}.convert.${className}Converter;
import ${package}.${pkgName}.dto.${className}DTO;
import ${package}.${pkgName}.model.${className};
import ${package}.${pkgName}.service.${className}Service;

/**
 * ${comments} 控制器
 *
 * @author ${author}
 * @date ${datetime}
 */
@RestController
@RequestMapping("${pathName}")
@Api(tags = {"${comments} Restful Http 接口"})
public class ${className}Controller {

    @Autowired
    private ${className}Service ${lowClassName}Service;

    /**
     * 条件查询，分页
     * @param condition 查询条件
     * @return 分页结果
     */
#if(${checkAuth})
    @PreAuthorize("hasAnyAuthority('${tableName}:${pathName}:list')")
#end
    public PageResult<${className}> list(@RequestParam Map<String, Object> condition){
        PageInfo<${className}> pageInfo = new PageInfo<>(${lowClassName}Service.findAll(condition));

        return PageResult.PageResultBuilder.aPageResult()
                .list(pageInfo.getList())
                .totalPageNum((int) pageInfo.getTotal())
                .build();
    }


    /**
     * 保存单个，推荐使用幂等的 PUT 方法，体现接口幂等性。为了方便习惯，也开放了 POST
     *
     * @param ${lowClassName}DTO 新增数据
     * @return 保存成功
     */
#if(${checkAuth})
    @PreAuthorize("hasAnyAuthority('resource:sysroleuser:save')")
#end
    public BaseResult save(@RequestBody ${className}DTO ${lowClassName}DTO){
        ${className} ${lowClassName} = ${className}Converter.CONVERTER.fromDTO(${lowClassName}DTO);
		${lowClassName}Service.save(${lowClassName});

        return BaseResult.success();
    }

    /**
     * 单个修改
     *
     * @param ${lowClassName}DTO 修改属性
     * @return 修改成功
     */
#if(${checkAuth})
    @PreAuthorize("hasAnyAuthority('resource:sysroleuser:update')")
#end
    public BaseResult update(@RequestBody ${className}DTO ${lowClassName}DTO){
        ${className} ${lowClassName} = ${className}Converter.CONVERTER.fromDTO(${lowClassName}DTO);
			${lowClassName}Service.update(${lowClassName});

        return BaseResult.success();
    }

    /**
     * 根据 id 删除单个
     *
     * @param ${pk.attributeName} id
     * @return 删除成功
     */
#if(${checkAuth})
    @PreAuthorize("hasAnyAuthority('resource:sysroleuser:delete')")
#end
    public BaseResult delete(@PathVariable Long ${pk.attributeName}){
        ${lowClassName}Service.deleteById(${pk.attributeName});
        return BaseResult.success();
    }

    /**
     * 根据 id 批量删除
     *
     * @param idList ids
     * @return 删除成功
     */
#if(${checkAuth})
    @PreAuthorize("hasAnyAuthority('resource:sysroleuser:delete')")
#end
    public BaseResult delete(List<Long> idList){
        if(CollectionUtils.isEmpty(idList)){
            return BaseResult.success();
        }
        ${lowClassName}Service.deleteByIds(idList);
        return BaseResult.success();
    }

}
