package org.hswebframework.web.example.simple;

import io.swagger.annotations.ApiOperation;
import org.hswebframework.web.AuthorizeException;
import org.hswebframework.web.authorization.Authentication;
import org.hswebframework.web.authorization.Permission;
import org.hswebframework.web.authorization.annotation.Authorize;
import org.hswebframework.web.authorization.annotation.RequiresDataAccess;
import org.hswebframework.web.commons.entity.Entity;
import org.hswebframework.web.commons.entity.PagerResult;
import org.hswebframework.web.commons.entity.param.QueryParamEntity;
import org.hswebframework.web.controller.QueryController;
import org.hswebframework.web.controller.message.ResponseMessage;
import org.hswebframework.web.entity.authorization.SimpleUserEntity;
import org.hswebframework.web.entity.authorization.UserEntity;
import org.hswebframework.web.logging.AccessLogger;
import org.hswebframework.web.model.authorization.UserModel;
import org.hswebframework.web.organizational.authorization.PersonnelAuthorization;
import org.hswebframework.web.service.QueryByEntityService;
import org.hswebframework.web.service.QueryService;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * TODO 完成注释
 *
 * @author zhouhao
 */
@RestController
@RequestMapping("/test")
@Authorize(permission = "test")
@AccessLogger("测试")
public class TestController implements QueryController<UserEntity, String, QueryParamEntity> {

    @GetMapping("/test1")
    @Authorize(action = "query")
    public ResponseMessage testSimple(Authentication authentication) {
        return ResponseMessage.ok(authentication).exclude(Authentication.class, "attributes");
    }

    @GetMapping("/test2")
    public ResponseMessage test2(Authentication authentication) {
        return ResponseMessage.ok(authentication);
    }

    @GetMapping("/testQuery")
    @Authorize
    @RequiresDataAccess(permission = "test", action = Permission.ACTION_QUERY)
    @ApiOperation("测试查询")
    @AccessLogger("查询")
    public ResponseMessage<QueryParamEntity> testQuery(QueryParamEntity entity) {

        /*
        @RequiresDataAccess 数据级别权限控制
        entity.terms 被嵌入查询条件
        */
        return ResponseMessage.ok(entity);
    }

    @PutMapping("/testUpdate/{id}")
    @RequiresDataAccess(permission = "test", action = Permission.ACTION_UPDATE)
    public ResponseMessage<UserModel> testUpdate(@PathVariable String id, @RequestBody UserModel model) {
        return ResponseMessage.ok(model);
    }

    @PutMapping("/test/testPersonnel")
    public ResponseMessage<PersonnelAuthorization> testPersonnel() {
        return ResponseMessage.ok(PersonnelAuthorization.current().get());
    }

    @Override
    public TestService getService() {
        return new TestService();
    }

    public static class TestService implements QueryByEntityService<UserEntity>, QueryService<UserEntity, String> {

        @Override
        public UserEntity selectByPk(String id) {
            SimpleUserEntity userEntity = new SimpleUserEntity();
            // 同一个用户
            userEntity.setCreatorId(Authentication.current().orElseThrow(AuthorizeException::new).getUser().getId());
            return userEntity;
        }

        @Override
        public List<UserEntity> selectByPk(List<String> id) {
            return null;
        }

        @Override
        public List<UserEntity> select() {
            return null;
        }

        @Override
        public int count() {
            return 0;
        }

        @Override
        public PagerResult<UserEntity> selectPager(Entity param) {
            return null;
        }

        @Override
        public List<UserEntity> select(Entity param) {
            return null;
        }

        @Override
        public int count(Entity param) {
            return 0;
        }

        @Override
        public UserEntity selectSingle(Entity param) {
            return null;
        }
    }
}

@@ -2484,32 +2484,212 @@
 (model);%0A    %7D%0A%0A
+%0A    @PutMapping(%22/testUpdateBatch%22)%0A    public ResponseMessage%3CList%3CUserModel%3E%3E testUpdate(@RequestBody List%3CUserModel%3E model) %7B%0A        return ResponseMessage.ok(model);%0A    %7D%0A%0A%0A
     @PutMapping(

优化test,增加泛型支持