package cn.com.ty.lift.ud.applyExpert.service.impl;

import cn.com.ty.lift.ud.userInfo.mapper.UserInfoMapper;
import cn.com.ty.lift.ud.userInfo.mapper.entity.UserInfoEntity;
import cn.com.ty.lift.ud.utils.QrCodeUtil;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.com.ty.lift.ud.applyExpert.controller.query.ApplyExpertQuery;
import cn.com.ty.lift.ud.applyExpert.mapper.ApplyExpertMapper;
import cn.com.ty.lift.ud.applyExpert.mapper.entity.ApplyExpert;
import cn.com.ty.lift.ud.applyExpert.service.IApplyExpertService;
import cn.com.ty.lift.ud.operation.mapper.UserLevelRuleMapper;
import cn.com.ty.lift.ud.operation.mapper.entity.UserLevelRuleEntity;
import cn.com.ty.lift.ud.userAccount.mapper.UserAccountMapper;
import cn.com.xwy.boot.web.dto.RestResponse;

/**
 * 专家申请表-ServiceImpl
 *
 * @author Xson
 * @date 2020-05-16
 */
@Service
@Transactional
public class ApplyExpertServiceImpl extends ServiceImpl<ApplyExpertMapper, ApplyExpert> implements IApplyExpertService {

    @Autowired
    private ApplyExpertMapper applyExpertMapper;
    @Autowired
    private UserAccountMapper userAccountMapper;
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private UserLevelRuleMapper userLevelRuleMapper;

    @Override
    public RestResponse examineById(ApplyExpert neDto) {
        ApplyExpert selectById = applyExpertMapper.selectById(neDto.getId());
        UserInfoEntity byUserId = userInfoMapper.getByUserId(selectById.getUserId());
        UserLevelRuleEntity selectById2 = userLevelRuleMapper.selectById(byUserId.getUserLevel());
        if (neDto.getCheckFlag() == 1) {
            applyExpertMapper.examineById(neDto.getId(), 1);
            userAccountMapper.examineById(selectById.getUserId(), 1);
            int code = (int)(((Math.random() * 9) + 1) * 100000);
            String qrCodeurl = null;
            try {
                qrCodeurl = QrCodeUtil.getQrCode(selectById.getUserId(), "/wode/vip");
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
//            userInfoMapper.examineById(selectById.getUserId(), 1, neDto.getServiceFee(), code, qrCodeurl,"专家");
            byUserId.setApplyFlag(1);
            byUserId.setServiceFee(neDto.getServiceFee());
            byUserId.setRequestCode(code);
            byUserId.setQrCodeUrl(qrCodeurl);
            byUserId.setUserLevelName("专家");
            byUserId.setStatuz(1);
            byUserId.setExpertLevel(1);
            byUserId.setPlatformInvitedFlag(1);
            byUserId.setRealName(selectById.getName());
            byUserId.setProficiency("擅长品牌："+selectById.getProficiencyBrandName()+"，擅长电器："+selectById.getGoodElectrical()+"，擅长机械："+selectById.getGoodMechanical());
            byUserId.setGender(selectById.getGender());
            byUserId.setAge(selectById.getAge());
            byUserId.setAverageScore(0);
            byUserId.setProficiencyBrand(selectById.getProficiencyBrand());
            if (selectById.getResidentPlace().split(",").length == 3) {
                byUserId.setLat(Double.parseDouble(selectById.getResidentPlace().split(",")[1]));
                byUserId.setLon(Double.parseDouble(selectById.getResidentPlace().split(",")[2]));
            }
            userInfoMapper.updateById(byUserId);

            return RestResponse.success(null, "审批通过");
        } else {
            applyExpertMapper.examineById(neDto.getId(), 0);
            userInfoMapper.examineById(selectById.getUserId(), 0, BigDecimal.ZERO, null, null,selectById2.getName());
            return RestResponse.fail(null, "审批驳回");
        }
    }

    @Override
    public RestResponse listExpert(ApplyExpertQuery query) {
        IPage page = query.page();
        List<ApplyExpert> list = applyExpertMapper.selectByQuery(query.condition(), query);
        for (ApplyExpert ae : list) {
            if (ae.getResidentPlace() != null) {
                ae.setResidentPlace(ae.getResidentPlace().split(",")[0]);
            }
        }
        int total = applyExpertMapper.countByQuery(query.condition());
        page.setRecords(list);
        page.setTotal(total);
        return RestResponse.success(page, "Queried successfully");
    }

}
