package cn.iocoder.yudao.module.shop.service.impl;

import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.partner.entity.PartnerBalance;
import cn.iocoder.yudao.module.partner.service.IPartnerBalanceService;
import cn.iocoder.yudao.module.partner.service.IPartnerWorkerService;
import cn.iocoder.yudao.module.system.dal.dataobject.permission.UserRoleDO;
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
import cn.iocoder.yudao.module.system.dal.mysql.permission.UserRoleMapper;
import cn.iocoder.yudao.module.system.service.user.AdminUserService;
import cn.iocoder.yudao.util.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.iocoder.yudao.module.partner.entity.PartnerWorker;
import cn.iocoder.yudao.module.shop.entity.ShopMallPartners;
import cn.iocoder.yudao.module.shop.mapper.ShopMallPartnersMapper;
import cn.iocoder.yudao.module.shop.service.IShopMallPartnersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import static cn.iocoder.yudao.module.inspection.utils.GetLocation.getAdd;

/**
 * 合作商管理Service业务层处理
 *
 * @author ruoyi
 * @date 2023-07-10
 */
@Service
public class ShopMallPartnersServiceImpl extends ServiceImpl<ShopMallPartnersMapper,ShopMallPartners> implements IShopMallPartnersService
{
    @Autowired
    private AdminUserService userService;
    @Autowired
    private IPartnerWorkerService workerService;
    @Autowired
    private UserRoleMapper userRoleMapper;

    @Autowired
    private IPartnerBalanceService partnerBalanceService;
    /**
     * 查询合作商管理
     *
     * @param partnerId 合作商管理主键
     * @return 合作商管理
     */
    @Override
    public ShopMallPartners selectShopMallPartnersByPartnerId(Long partnerId)
    {
        return baseMapper.selectShopMallPartnersByPartnerId(partnerId);
    }

    /**
     * 查询合作商管理列表
     *
     * @param shopMallPartners 合作商管理
     * @return 合作商管理
     */
    @Override
    public IPage<ShopMallPartners> selectShopMallPartnersList(Page<ShopMallPartners> page, ShopMallPartners shopMallPartners)
    {

        return baseMapper.selectShopMallPartnersList(page,shopMallPartners);
    }

    /**
     * 新增合作商管理
     *
     * @param shopMallPartners 合作商管理
     * @return 结果
     */
    @Override
    public int insertShopMallPartners(ShopMallPartners shopMallPartners)
    {
        if (shopMallPartners.getLatitude()!=null&&shopMallPartners.getLongitude()!=null){
            //省市区的赋值
            String[] address = getAdd(shopMallPartners.getLongitude(), shopMallPartners.getLatitude());
            shopMallPartners.setProvince(address[0]);
            shopMallPartners.setCity(address[1]);
            shopMallPartners.setArea(address[2]);
        }
        //未封禁
        shopMallPartners.setIsBanned("0");
        baseMapper.insert(shopMallPartners);
        //创建账户
        PartnerBalance partnerBalance =new PartnerBalance();
        partnerBalance.setPartnerId(shopMallPartners.getPartnerId());
        partnerBalance.setTotalRevenue(0l);
        partnerBalance.setAmount(0l);
        partnerBalance.setFrozenAmount(0l);
        partnerBalance.setWithdrawAmount(0l);
        partnerBalanceService.save(partnerBalance);
        return 1;
    }

    /**
     * 修改合作商管理
     *
     * @param shopMallPartners 合作商管理
     * @return 结果
     */
    @Override
    public int updateShopMallPartners(ShopMallPartners shopMallPartners)
    {
        if (shopMallPartners.getLatitude()!=null&&shopMallPartners.getLongitude()!=null){
            try {
                //省市区的赋值
                String address[] = getAdd(shopMallPartners.getLongitude(), shopMallPartners.getLatitude());
                shopMallPartners.setProvince(address[0]);
                shopMallPartners.setCity(address[1]);
                shopMallPartners.setArea(address[2]);
            }catch (Exception e){

            }

        }
        return baseMapper.updateShopMallPartners(shopMallPartners);
    }

    @Override
    public int setShopAdmin(ShopMallPartners shopMallPartners)
    {
        if (shopMallPartners.getLatitude()!=null&&shopMallPartners.getLongitude()!=null){
            try {
                //省市区的赋值
                String address[] = getAdd(shopMallPartners.getLongitude(), shopMallPartners.getLatitude());
                shopMallPartners.setProvince(address[0]);
                shopMallPartners.setCity(address[1]);
                shopMallPartners.setArea(address[2]);
            }catch (Exception e){

            }

        }
        if(null!=shopMallPartners.getUserId()){
            AdminUserDO user = userService.getUser(shopMallPartners.getUserId());
            userRoleMapper.deleteListByUserId(user.getId());
        }
        Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
        AdminUserDO user = userService.getUser(loginUserId);
        UserRoleDO userRoleDO = new UserRoleDO();
        userRoleDO.setRoleId(113L);
        userRoleDO.setUserId(loginUserId);
        userRoleMapper.insert(userRoleDO);

        //创建账户
        PartnerBalance partnerBalance =new PartnerBalance();
        partnerBalance.setPartnerId(shopMallPartners.getPartnerId());
        partnerBalance.setTotalRevenue(0L);
        partnerBalance.setAmount(0L);
        partnerBalance.setFrozenAmount(0L);
        partnerBalance.setWithdrawAmount(0L);
        partnerBalanceService.save(partnerBalance);
        //将店主加入员工
        PartnerWorker partnerWorker =new PartnerWorker();
        partnerWorker.setUserId(user.getId());
        partnerWorker.setPartnerId(shopMallPartners.getPartnerId());
        partnerWorker.setAvatar(user.getAvatar());
        partnerWorker.setWorkName(user.getNickname());
        partnerWorker.setWorkPhone(user.getMobile());
        workerService.save(partnerWorker);
        return baseMapper.updateShopMallPartners(shopMallPartners);
    }



    /**
     * 批量删除合作商管理
     *
     * @param partnerIds 需要删除的合作商管理主键
     * @return 结果
     */
    @Override
    public int deleteShopMallPartnersByPartnerIds(Long[] partnerIds)
    {
        return baseMapper.deleteShopMallPartnersByPartnerIds(partnerIds);
    }

    /**
     * 删除合作商管理信息
     *
     * @param partnerId 合作商管理主键
     * @return 结果
     */
    @Override
    public int deleteShopMallPartnersByPartnerId(Long partnerId)
    {
        return baseMapper.deleteShopMallPartnersByPartnerId(partnerId);
    }

    @Override
    public ShopMallPartners selectShopMallPartnersByUserId(Long userId,String type) {
        return baseMapper.selectShopMallPartnersByUserId(userId,type);
    }

    @Override
    public List<ShopMallPartners> selectShopMallPartnersListWx(ShopMallPartners shopMallPartners)
    {
        return baseMapper.selectShopMallPartnersListWx(shopMallPartners);
    }
}
