/*
 *      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.mall.service.impl;

import cn.hutool.core.util.DesensitizedUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.common.constant.CommonConstant;
import org.springblade.core.cache.utils.CacheUtil;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.utils.DigestUtil;
import org.springblade.core.tool.utils.WebUtil;
import org.springblade.modules.auth.utils.TokenUtil;
import org.springblade.modules.mall.entity.Store;
import org.springblade.modules.mall.vo.StoreVO;
import org.springblade.modules.mall.mapper.StoreMapper;
import org.springblade.modules.mall.service.IStoreService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springblade.modules.platform.service.UserAppService;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.entity.UserApp;
import org.springblade.modules.system.service.IUserService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

import static org.springblade.core.cache.constant.CacheConstant.USER_CACHE;

/**
 * 商家列表 服务实现类
 *
 * @author BladeX
 * @since 2023-06-01
 */
@Service
public class StoreServiceImpl extends ServiceImpl<StoreMapper, Store> implements IStoreService {

	@Resource
	private UserAppService userAppService;

	@Resource
	private IUserService userService;


	@Override
	public IPage<StoreVO> selectStorePage(IPage<StoreVO> page, StoreVO store) {
		return page.setRecords(baseMapper.selectStorePage(page, store));
	}

	@Override
	@Transactional
	public Boolean setStore(Store store) {
		userAppService.update(Wrappers.lambdaUpdate(UserApp.class).eq(UserApp::getUserId, store.getUserId()).set(UserApp::getIsStore, 1));
		User user = userService.getById(store.getUserId());
		if (StrUtil.isEmpty(user.getPassword())){//判断之前是否有密码
			userService.resetPasswordById(user.getId());//如果没有设置密码就给用户重置密码
		}
		return saveOrUpdate(store);
	}

	@Override
	@Transactional
	public Boolean addStore(Store store) {
		String tenantId = WebUtil.getRequest().getHeader(TokenUtil.TENANT_HEADER_KEY);
		User user = new User();
		user.setAccount(store.getTel());
		user.setPhone(store.getTel());
		user.setTenantId(tenantId);
		user.setPassword("123456");
		user.setName(DesensitizedUtil.mobilePhone(store.getTel()));
		CacheUtil.clear(USER_CACHE);
		userService.registerApp(user, null);
		store.setUserId(user.getId());
		if(ObjectUtil.isNotEmpty(store.getAuditState()) && store.getAuditState() == 1 && store.getState() == 1){
			userService.setRoleByRoleName(store.getUserId(), "供应商");
		}
		userAppService.update(Wrappers.lambdaUpdate(UserApp.class).eq(UserApp::getUserId, user.getId()).set(UserApp::getIsStore,1));
		return save(store);
	}

	@Override
	public Store getByUserId() {
		return getOne(Wrappers.lambdaQuery(Store.class)
				.eq(Store::getUserId, AuthUtil.getUserId())
				.eq(Store::getState, 1)
				.eq(Store::getAuditState, 1));
	}

	@Override
	@Transactional
	public void remove(Store store){
		userAppService.update(Wrappers.lambdaUpdate(UserApp.class).eq(UserApp::getUserId, store.getUserId()).set(UserApp::getIsStore, 0));
		removeById(store);
	}

}
