package com.pj.project.app_user;

import com.pj.api.client.level_one_server.LevelOneServerInterface;
import com.pj.api.client.level_two_server.LevelTwoServerInterface;
import com.pj.api.client.transport.TransportInterface;
import com.pj.api.dto.DriverDto;
import com.pj.api.dto.EnterpriseDto;
import com.pj.api.dto.PeopleDto;
import com.pj.api.dto.PurchaserDto;
import com.pj.common.core.exception.ServiceException;
import com.pj.project.app_user.vo.AppUserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author Mechrevo
 * @Date 2023 07 25 15 05
 **/
@Service
@Transactional(rollbackFor = Exception.class)
public class MethodAppUserService {

    /** 一级市场远程调用 */
    @Autowired
    private LevelOneServerInterface levelOneServerInterface;
    /** 二级市场远程调用 */
    @Autowired
    private LevelTwoServerInterface levelTwoServerInterface;
    /** 物流传输远程调用 */
    @Autowired
    private TransportInterface transportInterface;

    /**
     *     (1,"普通边民"),
     *     (2,"互市区边民组组长"),
     *     (3,"互市区商家"),
     *     (4,"司机"),
     *     (5,"国内收购商");
     */
    public AppUserVo getRoleName(AppUserVo appUser, Integer userType){
        if(userType == null || userType.toString().trim().equals(""))
            throw new ServiceException("身份错误!");
        //创建返回值对象
        AppUserVo appUserVo = new AppUserVo();
        BeanUtils.copyProperties(appUser,appUserVo);
        switch (userType){
            case 1:
                 appUserVo = setPeopleProperties(appUser);
                break;
            case 2:
                 appUserVo = setPeopleProperties(appUser);
                break;
            case 3:
                 appUserVo = setEnterprise(appUser);
                break;
            case 4:
                appUserVo = setDriver(appUser);
                break;
            case 5:
                appUserVo = setPurchaser(appUser);
                break;
            default:
                throw new RuntimeException("用户身份选择错误!");
        }


        return appUser;
    }

    /**
     * 二级市场商户
     * @param appUser
     * @return
     */
    AppUserVo setPurchaser(AppUserVo appUser){
        //查询国内收购商
        PurchaserDto purchaserDto = levelTwoServerInterface.getByPurchaserId(appUser.getFk());
        if(purchaserDto == null)throw new ServiceException("二级市场商户个人信息查询失败!");
        //设置基本属性
        appUser.setRoleName("国内收购商");
        appUser.setTradeAreaName("国内");
        appUser.setGenderName("待填");
        appUser.setAge(purchaserDto.getAge() + "岁");
        appUser.setAddress(purchaserDto.getAddress());

        return appUser;
    }

    /**
     * 司机
     * @param appUser
     * @return
     */
    AppUserVo setDriver(AppUserVo appUser){
        //查询货运司机
        DriverDto driverDto = transportInterface.getByDriverId(appUser.getFk());
        if(driverDto == null)throw new ServiceException("司机个人信息查询失败!");
        //设置基本属性
        appUser.setRoleName("司机");
        appUser.setTradeAreaName(driverDto.getTradeAreaName());
        appUser.setGenderName(driverDto.getSex() == 1? "男" : "女");
        appUser.setAge(driverDto.getAge() + "岁");
        appUser.setAddress(driverDto.getAddress());
        return appUser;
    }


    /**
     * 商家
     * @param appUser
     * @return
     */
    AppUserVo setEnterprise(AppUserVo appUser){
        //查询一级市场商户表
        EnterpriseDto enterprise = levelOneServerInterface.getEnterpriseById(appUser.getFk());
        if(enterprise == null)throw new ServiceException("一级市场商户个人信息查询失败!");
        //设置基本属性
        appUser.setRoleName("一级市场商户");
        appUser.setTradeAreaName("待填");
        appUser.setGenderName(enterprise.getSex() == 1? "男" : "女");
        appUser.setAge(enterprise.getAge() + "岁");
        appUser.setAddress(enterprise.getAddress());
        return appUser;
    }

    /**
     * 边民
     * @param appUser
     * @return
     */
    AppUserVo setPeopleProperties(AppUserVo appUser){
        //查询边民表
        PeopleDto peopleDto = levelOneServerInterface.getRpcById(appUser.getFk());
        if(peopleDto == null)throw new ServiceException("一级市场边民个人信息查询失败!");
        //设置基本属性
        appUser.setRoleName(peopleDto.getRole() == 1? "普通边民" : "边民组组长");
        appUser.setTradeAreaName(peopleDto.getTradeAreaName());
        appUser.setGroupName(peopleDto.getGroupName());
        appUser.setAge(peopleDto.getAge() + "岁");
        appUser.setGenderName(peopleDto.getGencd() == 1? "男" : "女");
        appUser.setAddress(peopleDto.getAddress());
        return appUser;
    }


}
