package com.softwarebr.nunu.web.admin;

import com.brframework.commoncms.annatotion.layout.ViewLayout;
import com.brframework.commoncms.core.Icons;
import com.brframework.commonwebadmin.aop.annotation.AOLog;
import com.softwarebr.nunu.entity.user.*;
import com.softwarebr.nunu.service.user.*;
import com.softwarebr.nunu.json.admin.user.*;
import com.brframework.commoncms.annatotion.AdminMenu;
import com.brframework.commoncms.annatotion.layout.PageLayout;
import com.brframework.commoncms.annatotion.option.CmsOption;
import com.brframework.commonweb.json.JSONResult;
import com.brframework.commonweb.json.Page;
import com.brframework.commonweb.json.PageParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import static com.brframework.commondb.core.ControllerPageHandle.convertTo;
import static com.brframework.commondb.core.ControllerPageHandle.jpaPageConvertToPage;

/**
 *
 * @Author lilin
 * @Date 2020-02-17 14:07:07
 */

@RestController
@Api(tags = "用户管理")
@AdminMenu(menuName = "用户管理", icon = Icons.USER, order = 4 )
@RequestMapping("/admin/access")
public class AdminUserController {

	@Autowired
	private MemberService memberService;

	@ApiOperation(value = "用户列表", notes = "用户列表", produces = "text/plain")
	@GetMapping("/v1/member/page")
	@AdminMenu(menuName = "用户管理")
	@PreAuthorize("hasRole('user-member-query')")
	@PageLayout(
			columnOptions = {
					@CmsOption(name = "查看", uriMappingMethod = "getMemberDetailV1"),
					@CmsOption(condition ="{status}==1", name = "禁用", uriMappingMethod = "prohibitMemberV1", alert = "确定禁用该用户吗?"),
					@CmsOption(condition ="{status}==0", name = "启用", uriMappingMethod = "enableMemberV1")
			}
	)
	public JSONResult<Page<MemberResult>> memberListV1(PageParam pageParam, MemberQueryParam param) {
		Page<MemberResult> page = jpaPageConvertToPage(memberService.page(pageParam, param), pageParam, MemberResult.class);
		for(MemberResult memberResult : page.getList()){
			if(null == memberResult.getNickname()){
				memberResult.setNickname(memberService.nicknameHandle(memberResult.getId()));
			}
			if(null == memberResult.getHeadImgUri()){
				memberResult.setHeadImgUri("https://aliyuncdn.beiru168.com/nunu/c065261e-8114-4ed8-a66a-bd7d8fe29a29.jpg");
			}
		}
		return JSONResult.ok(page);
	}

	@ApiOperation(value = "查看用户信息", notes = "查看用户信息", produces = "text/plain")
	@PreAuthorize("hasRole('user-member-query')")
	@GetMapping("/v1/member/detail/{id}")
	@ViewLayout(paddingMappingMethod = "getMemberDetailV1")
	public JSONResult<MemberResult> getMemberDetailV1(@PathVariable Long id) {
		MemberResult memberResult = convertTo(memberService.accessObject(id),  MemberResult.class);
		if(null == memberResult.getNickname()){
			memberResult.setNickname(memberService.nicknameHandle(memberResult.getId()));
		}
		if(null == memberResult.getHeadImgUri()){
			memberResult.setHeadImgUri("https://aliyuncdn.beiru168.com/nunu/c065261e-8114-4ed8-a66a-bd7d8fe29a29.jpg");
		}
		return JSONResult.ok(memberResult);
	}

	@ApiOperation(value = "禁用用户", notes = "禁用用户", produces = "text/plain")
	@PreAuthorize("hasRole('user-member-query')")
	@PostMapping("/v1/member/prohibit/{id}")
	@AOLog
	public JSONResult prohibitMemberV1(@PathVariable Long id) {
		memberService.prohibitMember(id);
		return JSONResult.ok();
	}

	@ApiOperation(value = "启用用户", notes = "启用用户", produces = "text/plain")
	@PreAuthorize("hasRole('user-member-query')")
	@PostMapping("/v1/member/enable/{id}")
	@AOLog
	public JSONResult enableMemberV1(@PathVariable Long id) {
		memberService.enableMember(id);
		return JSONResult.ok();
	}

}