package com.ruoyi.mall.service.impl;

import java.util.*;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.AMAPUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.mall.domain.MallSalesman;
import com.ruoyi.mall.domain.vo.CascaderVO;
import com.ruoyi.mall.mapper.MallRegionMapper;
import com.ruoyi.mall.mapper.MallShopSupplierMapper;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.mapper.MallShopShopMapper;
import com.ruoyi.mall.domain.MallShopShop;
import com.ruoyi.mall.service.IMallShopShopService;

import javax.annotation.Resource;

/**
 * 商家管理Service业务层处理
 *
 * @author ruoyi
 * @date 2024-03-28
 */
@Service
public class MallShopShopServiceImpl implements IMallShopShopService
{
    @Resource
    private MallShopShopMapper mallShopShopMapper;
    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private SysUserRoleMapper sysUserRoleMapper;
    @Resource
    private MallRegionMapper mallRegionMapper;
    @Resource
    private AMAPUtils amapUtils;
    @Resource
    private MallShopSupplierMapper supplierMapper;

    /**
     * 查询商家管理
     *
     * @param shopId 商家管理主键
     * @return 商家管理
     */
    @Override
    public MallShopShop selectMallShopShopByShopId(Long shopId)
    {
        return mallShopShopMapper.selectMallShopShopByShopId(shopId);
    }

    /**
     * 查询商家管理列表
     *
     * @param mallShopShop 商家管理
     * @return 商家管理
     */
    @Override
    public List<MallShopShop> selectMallShopShopList(MallShopShop mallShopShop)
    {
        return mallShopShopMapper.selectMallShopShopList(mallShopShop);
    }

    /**
     * 新增商家管理
     *
     * @param mallShopShop 商家管理
     * @return 结果
     */
    @Override
    public int insertMallShopShop(MallShopShop mallShopShop)
    {
        String account = mallShopShop.getShopAccount();// 账号
        String password = mallShopShop.getShopPassword();// 密码

        // 新增系统用户
        SysUser sysUser = new SysUser();
        sysUser.setUserName(account);
        sysUser.setNickName(mallShopShop.getShopName());
        sysUser.setPhonenumber(mallShopShop.getShopPhone());
        sysUser.setStatus(mallShopShop.getShopStatus().toString());
        sysUser.setPassword(SecurityUtils.encryptPassword(password));
        sysUserMapper.insertUser(sysUser);

        // 新增用户与角色管理
        List<SysUserRole> userRoleList = new ArrayList<>();
        SysUserRole ur = new SysUserRole();
        ur.setUserId(sysUser.getUserId());
        ur.setRoleId(5L);// 账号类型（5-商家
        userRoleList.add(ur);
        sysUserRoleMapper.batchUserRole(userRoleList);

        mallShopShop.setSysUserId(sysUser.getUserId());
        mallShopShop.setDelFlag(0);
        mallShopShop.setCreateAt(DateUtils.getTime());

        if (mallShopShop.getProvinceId() != null && mallShopShop.getCityId() != null && mallShopShop.getAreaId() != null){
            String provinceName = mallRegionMapper.getRegionName(mallShopShop.getProvinceId());
            String cityName = mallRegionMapper.getRegionName(mallShopShop.getCityId());
            String areaName = mallRegionMapper.getRegionName(mallShopShop.getAreaId());
            mallShopShop.setProvinceName(provinceName);
            mallShopShop.setCityName(cityName);
            mallShopShop.setAreaName(areaName);

            //坐标
            AjaxResult shopLonLat = amapUtils.getShopLonLat(cityName, areaName, mallShopShop.getAddress());
            Integer code = MapUtils.getInteger(shopLonLat, "code");
            if (code == 200){
                String longitude = MapUtils.getString(shopLonLat, "longitude");
                String latitude = MapUtils.getString(shopLonLat, "latitude");
                mallShopShop.setLongitude(longitude);
                mallShopShop.setLatitude(latitude);
            }
        }
        return mallShopShopMapper.insertMallShopShop(mallShopShop);
    }

    /**
     * 修改商家管理
     *
     * @param mallShopShop 商家管理
     * @return 结果
     */
    @Override
    public int updateMallShopShop(MallShopShop mallShopShop)
    {
        String account = mallShopShop.getShopAccount();// 账号

        // 修改系统用户
        SysUser sysUser = new SysUser();
        sysUser.setUserId(mallShopShop.getSysUserId());
        sysUser.setUserName(account);
        sysUser.setNickName(mallShopShop.getShopName());
        sysUser.setPhonenumber(mallShopShop.getShopPhone());
        sysUser.setStatus(mallShopShop.getShopStatus().toString());
        sysUser.setUpdateTime(DateUtils.getNowDate());
        sysUserMapper.updateUser(sysUser);


        mallShopShop.setUpdateAt(DateUtils.getTime());

        if (mallShopShop.getProvinceId() != null && mallShopShop.getCityId() != null && mallShopShop.getAreaId() != null){
            String provinceName = mallRegionMapper.getRegionName(mallShopShop.getProvinceId());
            String cityName = mallRegionMapper.getRegionName(mallShopShop.getCityId());
            String areaName = mallRegionMapper.getRegionName(mallShopShop.getAreaId());
            mallShopShop.setProvinceName(provinceName);
            mallShopShop.setCityName(cityName);
            mallShopShop.setAreaName(areaName);

            //坐标
            AjaxResult shopLonLat = amapUtils.getShopLonLat(cityName, areaName, mallShopShop.getAddress());
            Integer code = MapUtils.getInteger(shopLonLat, "code");
            if (code == 200){
                String longitude = MapUtils.getString(shopLonLat, "longitude");
                String latitude = MapUtils.getString(shopLonLat, "latitude");
                mallShopShop.setLongitude(longitude);
                mallShopShop.setLatitude(latitude);
            }
        }
        return mallShopShopMapper.updateMallShopShop(mallShopShop);
    }

    /**
     * 批量删除商家管理
     *
     * @param shopIds 需要删除的商家管理主键
     * @return 结果
     */
    @Override
    public int deleteMallShopShopByShopIds(Long[] shopIds)
    {
        return mallShopShopMapper.deleteMallShopShopByShopIds(shopIds);
    }

    /**
     * 删除商家管理信息
     *
     * @param shopId 商家管理主键
     * @return 结果
     */
    @Override
    public int deleteMallShopShopByShopId(Long shopId)
    {
        return mallShopShopMapper.deleteMallShopShopByShopId(shopId);
    }

    /**
     * 根据商家名称查询商家管理
     *
     * @param shopName 商家名称
     * @return 商家管理
     */
    @Override
    public MallShopShop selectMallShopShopByShopName(String shopName) {
        return mallShopShopMapper.selectMallShopShopByShopName(shopName);
    }

    /**
     * 根据商家账号查询商家管理
     *
     * @param shopAccount 商家账号
     * @return 商家管理
     */
    @Override
    public MallShopShop selectMallShopShopByShopAccount(String shopAccount) {
        return mallShopShopMapper.selectMallShopShopByShopAccount(shopAccount);
    }

    /**
     * 根据系统用户ID查询商家管理
     *
     * @param sysUserId 系统用户ID
     * @return 商家管理
     */
    @Override
    public MallShopShop selectMallShopShopBySysUserId(Long sysUserId) {
        return mallShopShopMapper.selectMallShopShopBySysUserId(sysUserId);
    }

    /**
     * 后台重置商家密码
     * @return 商家
     */
    @Override
    public AjaxResult resetPassword(Map<String,Object> param){
        Long shopId = MapUtils.getLong(param, "shopId");
        String newPassword = MapUtils.getString(param, "newPassword");

        MallShopShop mallShopShop = mallShopShopMapper.selectMallShopShopByShopId(shopId);
        if (mallShopShop != null) {
            // 修改系统用户
            SysUser sysUser = new SysUser();
            sysUser.setUserId(mallShopShop.getSysUserId());
            sysUser.setPassword(SecurityUtils.encryptPassword(newPassword));
            sysUser.setUpdateTime(DateUtils.getNowDate());
            sysUserMapper.updateUser(sysUser);

            mallShopShop.setShopPassword(newPassword);
            mallShopShop.setUpdateAt(DateUtils.getTime());
            mallShopShopMapper.updateMallShopShop(mallShopShop);
            return AjaxResult.success("重置成功");
        } else {
            return AjaxResult.error("原密码错误");
        }

    }

    /**
     * 查询商家管理
     *
     * @param params 参数
     * @return 商家管理
     */
    @Override
    public List<Map<String,Object>> getShopNearestDistance(Map<String, Object> params) {
        return mallShopShopMapper.getShopNearestDistance(params);
    }

    /**
     * 商家列表(无经纬度)
     * @return
     */
    @Override
    public List<Map<String, Object>> queryShopList() {
        return mallShopShopMapper.queryShopList();
    }

    /**
     * 所有店铺和供应商列表
     * @return
     */
    @Override
    public List<CascaderVO> selectAllShopList() {
        List<CascaderVO> list = new ArrayList<>();
        // 店铺列表
        List<CascaderVO> mallShopShops = mallShopShopMapper.selectAllShopList();
        if (!mallShopShops.isEmpty()){
            for (CascaderVO mallShopShop : mallShopShops) {
                // 查询店铺下供应商
                List<CascaderVO> cascaderVOS = supplierMapper.selectMallShopSupplierByShopId(mallShopShop.getId());
                if (!cascaderVOS.isEmpty()){
                    mallShopShop.setChildren(cascaderVOS);
                }else{
                    mallShopShop.setChildren(new ArrayList<>());
                }
            }
        }
        return mallShopShops;
    }
}
