/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.ldt.appaccount.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import org.springblade.common.constant.SystemConstant;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.DigestUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.system.entity.User;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.modules.ldt.appaccount.entity.AppAccount;
import org.springblade.modules.ldt.appaccount.vo.AppAccountVO;
import org.springblade.modules.ldt.appaccount.service.IAppAccountService;
import org.springblade.core.boot.ctrl.BladeController;

/**
 *  控制器
 *
 * @author BladeX
 * @since 2021-08-08
 */
@RestController
@AllArgsConstructor
@RequestMapping("cyzh-ldt/appaccount")
@Api(value = "", tags = "接口")
public class AppAccountController extends BladeController {

	private final IAppAccountService appAccountService;
	private final RedisTemplate<String,String> redisTemplate;
	private static final String REDIS_KEY_CODE = "sms:code:";

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入appAccount")
	public R<AppAccount> detail(AppAccount appAccount) {
		AppAccount detail = appAccountService.getOne(Condition.getQueryWrapper(appAccount));
		return R.data(detail);
	}

	/**
	 * 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入appAccount")
	public R<IPage<AppAccount>> list(AppAccount appAccount, Query query) {
		IPage<AppAccount> pages = appAccountService.page(Condition.getPage(query), Condition.getQueryWrapper(appAccount));
		return R.data(pages);
	}

	/**
	 * 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入appAccount")
	public R<IPage<AppAccountVO>> page(AppAccountVO appAccount, Query query) {
		IPage<AppAccountVO> pages = appAccountService.selectAppAccountPage(Condition.getPage(query), appAccount);
		return R.data(pages);
	}

	/**
	 * 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入appAccount")
	public R save(@Valid @RequestBody AppAccount appAccount) {
		return R.status(appAccountService.save(appAccount));
	}

	/**
	 * 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入appAccount")
	public R update(@Valid @RequestBody AppAccount appAccount) {
		return R.status(appAccountService.updateById(appAccount));
	}

	/**
	 * 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入appAccount")
	public R submit(@Valid @RequestBody AppAccount appAccount) {
		return R.status(appAccountService.saveOrUpdate(appAccount));
	}


	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(appAccountService.deleteLogic(Func.toLongList(ids)));
	}

	/**
	 * 商户商场登录
	 * type: 1-商场 2-商户
	 */
	@PostMapping("/login")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "商户商场登录", notes = "传入ids")
	public R login(@RequestBody AppAccount appAccount) {
		Assert.notNull(appAccount.getPassword(),"密码不能为空");
		Assert.notNull(appAccount.getPhone(),"手机号码不能为空");
		Assert.notNull(appAccount.getType(),"登录类型不能为空");
		AppAccount isExist = appAccountService.getBaseMapper().selectOne(new QueryWrapper<>(new AppAccount()).lambda()
			.eq(AppAccount::getPhone, appAccount.getPhone()).eq(AppAccount::getType, appAccount.getType())
			.eq(AppAccount::getPassword,DigestUtil.hex(appAccount.getPassword())));
		if(isExist != null){
			return R.status(true);
		}
		return R.status(false);
	}

	/**
	 * 忘记密码
	 * type: 1-商场 2-商户
	 */
	@PostMapping("/forgetPassword")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "忘记密码", notes = "传入ids")
	public R login(@RequestBody AppAccountVO appAccount) {
		Assert.notNull(appAccount.getPassword(),"密码不能为空");
		Assert.notNull(appAccount.getPhone(),"手机号码不能为空");
		Assert.notNull(appAccount.getType(),"登录类型不能为空");
		String redisCodeKey = REDIS_KEY_CODE + appAccount.getPhone();
		String codeValue = redisTemplate.opsForValue().get(redisCodeKey);
		if(codeValue!=null&&codeValue.equals(appAccount.getCode())){
			AppAccount isExist = appAccountService.getBaseMapper().selectOne(new QueryWrapper<>(new AppAccount()).lambda()
				.eq(AppAccount::getPhone, appAccount.getPhone()).eq(AppAccount::getType, appAccount.getType()));
			Assert.notNull(isExist,"账号不存在，请先注册");
			isExist.setPassword(DigestUtil.hex(appAccount.getPassword()));
			return R.status(appAccountService.updateById(isExist));
		}else {
			return R.fail("验证码错误");
		}
	}

	/**
	 * 商家账号注册
	 *
	 */
	@PostMapping("/shop/register")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "忘记密码", notes = "传入ids")
	public R register(@RequestBody AppAccountVO appAccount) {
		Assert.notNull(appAccount.getPassword(),"密码不能为空");
		Assert.notNull(appAccount.getPhone(),"手机号码不能为空");
		String redisCodeKey = REDIS_KEY_CODE + appAccount.getPhone();
		String codeValue = redisTemplate.opsForValue().get(redisCodeKey);
		if(codeValue!=null&&codeValue.equals(appAccount.getCode())){
			AppAccount isExist = appAccountService.getBaseMapper().selectOne(new QueryWrapper<>(new AppAccount()).lambda()
				.eq(AppAccount::getPhone, appAccount.getPhone()).eq(AppAccount::getType, SystemConstant.MALLORSHOP.SHOP.getValue()));
			if(isExist != null){
				isExist.setPassword(DigestUtil.hex(appAccount.getPassword()));
				return R.status(appAccountService.updateById(isExist));
			}
			AppAccount appAccount1 = new AppAccount();
			appAccount1.setPassword(DigestUtil.hex(appAccount.getPassword()));
			appAccount1.setPhone(appAccount.getPhone());
			appAccount1.setType(SystemConstant.MALLORSHOP.SHOP.getValue());

			return R.status(appAccountService.save(appAccount1));
		}else {
			return R.fail("验证码错误");
		}
	}

	/**
	 * 商户商场设置密码/修改密码
	 * type: 1-商场 2-商户
	 */
	@PostMapping("/setPassword")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "商户商场设置密码/修改密码", notes = "传入ids")
	public R setPassword(@RequestBody AppAccountVO appAccount) {
		Assert.notNull(appAccount.getPassword(),"密码不能为空");
		Assert.notNull(appAccount.getPhone(),"手机号码不能为空");
		Assert.notNull(appAccount.getType(),"登录类型不能为空");
		AppAccount isExist = appAccountService.getBaseMapper().selectOne(new QueryWrapper<>(new AppAccount()).lambda()
			.eq(AppAccount::getPhone, appAccount.getPhone()).eq(AppAccount::getType, appAccount.getType()));
		if(isExist != null){
			//修改密码
			Assert.notNull(appAccount.getOldPassword(),"旧密码不能为空");
			Assert.isTrue(isExist.getPassword().equals(DigestUtil.hex(appAccount.getOldPassword())),"旧密码错误");
			//设置新的密码
			isExist.setPassword(DigestUtil.hex(appAccount.getPassword()));
			return R.status(appAccountService.updateById(isExist));
		}else{
			AppAccount newObject = new AppAccount();
			newObject.setPassword(DigestUtil.hex(appAccount.getPassword()));
			newObject.setPhone(appAccount.getPhone());
			newObject.setType(appAccount.getType());
			return R.status(appAccountService.save(newObject));
		}

	}


//	public static void main(String[] args) {
//
//		System.out.println((DigestUtil.hex("f810af89007929fe7642ca763a137586")));;
//
//	}

}
