package org.springblade.face.controller;


import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.validation.Valid;

import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.face.data.LoginSetData;
import org.springblade.face.data.TagRecordData;
import org.springblade.face.entity.TagRecord;
import org.springblade.face.query.PageQuery;
import org.springblade.face.service.ITagRecordService;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;

import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * 标记记录表 前端控制器
 * </p>
 *
 * @author zbc
 * @since 2021-07-05
 */
@RestController
@RequestMapping("/tagRecord")
@Slf4j
@Api(value = "标记记录", tags = "标记记录接口")
public class TagRecordController {

	@Autowired
	private ITagRecordService tagRecordService;

	@Autowired
	private IUserService userService;


	@GetMapping("/page")
	@ApiOperation(value = "分页", notes = "传入query")
	public R<IPage<TagRecord>> page(PageQuery pageQuery, Query query) {
		IPage<TagRecord> page = Condition.getPage(query);
		IPage<TagRecord> pages = tagRecordService.page(page,new LambdaQueryWrapper<TagRecord>()
			.eq(TagRecord::getTagUserId,pageQuery.getUserId())
			.orderByDesc(TagRecord::getCreateTime));
		return R.data(pages);
	}


	@PostMapping("/addTags")
	@ApiOperation(value = "新增标记记录", notes = "传入tagRecord")
	public R save(@Valid @RequestBody TagRecordData tagRecord) {
		if(StrUtil.isBlank(tagRecord.getUserIds())){
			return R.fail("UserIds不能为空");
		}
		try {
			BladeUser user = AuthUtil.getUser();
			if(user != null){
				String[] split = tagRecord.getUserIds().split(",");
				for (String s : split) {
					User us = userService.getById(s);
					us.setLoginStatus(0);
					userService.updateById(us);

					TagRecord tag = new TagRecord();
					tag.setRemoteAccount(user.getAccount());
					tag.setRemoteName(user.getUserName());
					tag.setTagUserId(Long.valueOf(s));
					tag.setRemark(tagRecord.getRemark());
					tag.setCreateTime(new Date());
					tagRecordService.save(tag);
				}
				return R.success("标记成功");
			}
		}catch (Exception e){
			e.printStackTrace();
			return R.fail("标记异常");
		}
		return R.fail("请登录后再操作");
	}



	@GetMapping("/select/list")
	@ApiOperation(value = "用户下拉列表")
	public R selectList() {
		List<User> list = userService.list(
			new LambdaQueryWrapper<User>()
				.eq(User::getIsDeleted, 0)
		);
		List<Map<String, Object>> data = list.stream()
			.map(obj -> {
				Map<String, Object> map = new HashMap<>();
				map.put("id", obj.getId());
				map.put("name", obj.getName());
				return map;
			}).collect(Collectors.toList());
		return R.data(data);
	}


	@PostMapping("/loginSet")
	@ApiOperation(value = "设置账号登录限制")
	public R loginSet(@RequestBody LoginSetData loginSetData) {
		if(StrUtil.isBlank(loginSetData.getUserId()) || loginSetData.getIsLogin() == null){
			return R.fail("参数异常");
		}
		User user = userService.getById(loginSetData.getUserId());
		if(user != null){
			user.setIsLogin(loginSetData.getIsLogin());
			if(userService.updateById(user)){
				return R.success("设置成功");
			}
		}
		return R.fail("设置失败");
	}


	@PostMapping("/wxLoginSet")
	@ApiOperation(value = "设置账号微信登录限制")
	public R wxLoginSet(@RequestBody LoginSetData loginSetData) {
		if(StrUtil.isBlank(loginSetData.getUserId()) || loginSetData.getIsWxLogin() == null){
			return R.fail("参数异常");
		}
		User user = userService.getById(loginSetData.getUserId());
		if(user != null){
			user.setIsWxLogin(loginSetData.getIsWxLogin());
			if(userService.updateById(user)){
				return R.success("设置成功");
			}
		}
		return R.fail("设置失败");
	}


}

