package com.ruoyi.mall.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.ApiResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.mall.domain.MallMemberCms;
import com.ruoyi.mall.domain.MallMemberUser;
import com.ruoyi.mall.mapper.MallMemberCmsMapper;
import com.ruoyi.mall.mapper.MallMemberUserMapper;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.mapper.MallMemberRegisterMapper;
import com.ruoyi.mall.domain.MallMemberRegister;
import com.ruoyi.mall.service.IMallMemberRegisterService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 用户注册Service业务层处理
 *
 * @author ruoyi
 * @date 2022-06-28
 */
@Service
public class MallMemberRegisterServiceImpl implements IMallMemberRegisterService
{
    @Resource
    private MallMemberRegisterMapper mallMemberRegisterMapper;
    @Resource
    private MallMemberUserMapper mallMemberUserMapper;

    /**
     * 提交注册信息
     * @param param
     * @return
     */
    @Override
    public ApiResult submitMemberRegister(Map<String,Object> param){
        //查询此手机号是否已注册
        MallMemberUser memberUser = mallMemberUserMapper.queryByPhone(MapUtils.getString(param, "registerPhone"));
        if (ObjectUtils.isNotEmpty(memberUser)){
            return ApiResult.error("此手机号已被他人注册");
        }

        MallMemberRegister mallMemberRegister = new MallMemberRegister();
        mallMemberRegister.setRegisterPhone(MapUtils.getString(param, "registerPhone"));
        mallMemberRegister.setShopName(MapUtils.getString(param, "shopName"));
        mallMemberRegister.setPersonName(MapUtils.getString(param, "personName"));
        mallMemberRegister.setProvince(MapUtils.getString(param, "province"));
        mallMemberRegister.setCity(MapUtils.getString(param, "city"));
        mallMemberRegister.setArea(MapUtils.getString(param, "area"));
        mallMemberRegister.setAddress(MapUtils.getString(param, "address"));
        mallMemberRegister.setPassword(SecurityUtils.encryptPassword(MapUtils.getString(param, "password")));
//        mallMemberRegister.setShopImage(MapUtils.getString(param, "shopImage"));
        mallMemberRegister.setStatus(0);//状态（0-待审核，1-通过，2-拒绝）

        mallMemberRegister.setCreateAt(DateUtils.getTime());
        mallMemberRegister.setDelFlag(0);
        return ApiResult.toAjax(mallMemberRegisterMapper.insertMallMemberRegister(mallMemberRegister));
    }

    /**
     * 查询用户注册
     *
     * @param id 用户注册主键
     * @return 用户注册
     */
    @Override
    public MallMemberRegister selectMallMemberRegisterById(Integer id)
    {
        return mallMemberRegisterMapper.selectMallMemberRegisterById(id);
    }

    /**
     * 查询用户注册列表
     *
     * @param mallMemberRegister 用户注册
     * @return 用户注册
     */
    @Override
    public List<MallMemberRegister> selectMallMemberRegisterList(MallMemberRegister mallMemberRegister)
    {
        return mallMemberRegisterMapper.selectMallMemberRegisterList(mallMemberRegister);
    }

    /**
     * 新增用户注册
     *
     * @param mallMemberRegister 用户注册
     * @return 结果
     */
    @Override
    public int insertMallMemberRegister(MallMemberRegister mallMemberRegister)
    {
        return mallMemberRegisterMapper.insertMallMemberRegister(mallMemberRegister);
    }

    /**
     * 修改用户注册
     *
     * @param mallMemberRegister 用户注册
     * @return 结果
     */
    @Override
    public int updateMallMemberRegister(MallMemberRegister mallMemberRegister)
    {
        return mallMemberRegisterMapper.updateMallMemberRegister(mallMemberRegister);
    }

    /**
     * 审核用户注册
     *
     * @param mallMemberRegister 用户注册
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult examine(MallMemberRegister mallMemberRegister)
    {
        try {
            //审核通过
            if (mallMemberRegister.getStatus() == 1){

                //查询此手机号是否已注册
                MallMemberUser memberUser = mallMemberUserMapper.queryByPhone(mallMemberRegister.getRegisterPhone());
                if (ObjectUtils.isNotEmpty(memberUser)){
                    return AjaxResult.error("此手机号已被他人注册");
                }

                //创建用户
                MallMemberUser mallMemberUser = new MallMemberUser();
                mallMemberUser.setPhone(mallMemberRegister.getRegisterPhone());
                mallMemberUser.setName(mallMemberRegister.getPersonName());
                mallMemberUser.setPassword(mallMemberRegister.getPassword());
                mallMemberUser.setSalesmanId(mallMemberRegister.getSalesmanId());
                mallMemberUser.setToken(UUID.randomUUID().toString(true));
                mallMemberUser.setStatus("0");
                mallMemberUser.setProvince(mallMemberRegister.getProvince());
                mallMemberUser.setCity(mallMemberRegister.getCity());
                mallMemberUser.setArea(mallMemberRegister.getArea());
                mallMemberUser.setAddress(mallMemberRegister.getAddress());
                mallMemberUser.setShopImage(mallMemberRegister.getShopImage());
                mallMemberUser.setShopName(mallMemberRegister.getShopName());
                mallMemberUser.setPersonName(mallMemberRegister.getPersonName());
                mallMemberUser.setCreateAt(DateUtils.getTime());
                mallMemberUser.setDelFlag("0");
                mallMemberUserMapper.insertMallMemberUser(mallMemberUser);

                mallMemberRegisterMapper.updateMallMemberRegister(mallMemberRegister);
            }
            return AjaxResult.success();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 批量删除用户注册
     *
     * @param ids 需要删除的用户注册主键
     * @return 结果
     */
    @Override
    public int deleteMallMemberRegisterByIds(Integer[] ids)
    {
        return mallMemberRegisterMapper.deleteMallMemberRegisterByIds(ids);
    }

    /**
     * 删除用户注册信息
     *
     * @param id 用户注册主键
     * @return 结果
     */
    @Override
    public int deleteMallMemberRegisterById(Integer id)
    {
        return mallMemberRegisterMapper.deleteMallMemberRegisterById(id);
    }
}
