package cn.com.ty.lift.system.user.service.impl;

import cn.com.ty.lift.common.constants.ApiConstants;
import cn.com.ty.lift.common.utils.ProjectUtils;
import cn.com.ty.lift.system.user.dao.entity.Expert;
import cn.com.ty.lift.system.user.dao.entity.UserAccount;
import cn.com.ty.lift.system.user.dao.entity.UserInfo;
import cn.com.ty.lift.system.user.dao.entity.model.ExpertRequest;
import cn.com.ty.lift.system.user.dao.entity.model.ExpertResponse;
import cn.com.ty.lift.system.user.dao.entity.model.UserResponse;
import cn.com.ty.lift.system.user.dao.mapper.ExpertMapper;
import cn.com.ty.lift.system.user.service.IExpertService;
import cn.com.ty.lift.system.user.service.IUserAccountService;
import cn.com.ty.lift.system.user.service.IUserInfoService;
import cn.com.ty.lift.system.user.service.IUserService;
import cn.com.xwy.boot.web.dto.RestResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 专家表 服务实现类
 * </p>
 *
 * @author huangyuan
 * @since 2020-01-02
 */
@Service
public class ExpertServiceImpl extends ServiceImpl<ExpertMapper, Expert> implements IExpertService {

    @Resource
    private IUserAccountService userAccountService;

    @Resource
    private IUserInfoService userInfoService;

    @Resource
    private IUserService userService;

    @Override
    public RestResponse expertList(ExpertRequest expertRequest) {
        IPage expertPage = new Page(expertRequest.getPageNum(), expertRequest.getPageSize());
        expertPage = this.page(expertPage, new QueryWrapper<Expert>()
                .like(StringUtils.isNotBlank(expertRequest.getName()), "name", expertRequest.getName())
                .eq("delete_flag", ApiConstants.DELETE_NO)
        );

        List<Expert> expertList = expertPage.getRecords();
        //封装专家信息
        if (expertList != null && expertList.size() > 0) {
            List<ExpertResponse> expertResponseList = new ArrayList<>();
            List<Long> userIdList = ProjectUtils.getAttrList(expertList, "userId", null);
            Map<Long, UserResponse> userIdToUserResponseList = userService.getUserResponseByUserIdList(userIdList);
            for (Expert expert : expertList) {
                ExpertResponse expertResponse = new ExpertResponse();
                //设置专家信息
                expertResponse.setId(expert.getId());
                expertResponse.setName(expert.getName());
                expertResponse.setDescription(expert.getDescription());
                expertResponse.setUserId(expert.getUserId());
                //设置用户信息
                UserResponse userResponse = userIdToUserResponseList.get(expert.getUserId());
                if(userResponse != null){
                    expertResponse.setAvatarUrl(userResponse.getAvatarUrl());
                    expertResponse.setMobile(userResponse.getMobile());
                }
                expertResponseList.add(expertResponse);
            }
            expertPage.setRecords(expertResponseList);
            return RestResponse.success(expertPage, ApiConstants.RESULT_SUCCESS, null);
        }

        return RestResponse.success();
    }

    @Override
    @Transactional
    public RestResponse add(ExpertRequest expertRequest) {
        UserAccount userAccount = new UserAccount();
        userAccount.setMobile(expertRequest.getMobile());
        //设置用户为专家
        userAccount.setExpertFlag(ApiConstants.UserConstants.IS_EXPERT);
        userAccount.setType(ApiConstants.UserConstants.TYPE_USER);
        userAccountService.save(userAccount);
        //设置专家基础信息
        UserInfo userInfo = new UserInfo();
        userInfo.setMobile(expertRequest.getMobile());
        userInfo.setUserId(userAccount.getUserId());
        userInfoService.save(userInfo);
        //设置专家信息
        Expert expert = new Expert();
        expert.setName(expertRequest.getName());
        expert.setDescription(expertRequest.getDescription());
        expert.setUpdateTime(LocalDateTime.now());
        expert.setUserId(userAccount.getUserId());
        this.save(expert);
        return RestResponse.success(null, ApiConstants.RESULT_SUCCESS, "保存专家信息成功");
    }

}
