package com.huiju.estateagents.property.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huiju.estateagents.base.ResponseBean;
import com.huiju.estateagents.center.taUser.mapper.TaUserMapper;
import com.huiju.estateagents.common.BeanTools;
import com.huiju.estateagents.property.dao.*;
import com.huiju.estateagents.property.model.*;
import com.huiju.estateagents.property.service.ITpActivitySignUpService;
import com.huiju.estateagents.property.vo.ActivitySignUpVO;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 物业web端活动报名表 服务实现类
 * </p>
 *
 * @author weiximei
 * @since 2019-01-03
 */
@Service
public class TpActivitySignUpServiceImpl extends ServiceImpl<TpActivitySignUpMapper, TpActivitySignUp> implements ITpActivitySignUpService {

    @Autowired
    private TpActivityMapper tpActivityMapper;

    @Autowired
    private TpActivitySignUpMapper tpActivitySignUpMapper;

//    @Autowired
//    private TaUserMapper taUserMapper;

    @Autowired
    private TpBuildingOwnerInfoMapper tpBuildingOwnerInfoMapper;

    @Autowired
    private TaSysRoleMapper taSysRoleMapper;

    @Autowired
    private TaUserVerifyMapper taUserVerifyMapper;



    @Override
    public ResponseBean getSignUpUser(String parameter) {
        ResponseBean responseBean = new ResponseBean();

        JSONObject jsonObject = JSONObject.parseObject(parameter);
        Integer activityId = jsonObject.getInteger("id");
        Integer pageNum = jsonObject.getInteger("pageNum");
        Integer pageSize = jsonObject.getInteger("pageSize");
        String userName = jsonObject.getString("userName");
        String phone = jsonObject.getString("phone");

        TpActivity tpActivity = tpActivityMapper.selectById(activityId);
        if (null == tpActivity) {
            responseBean.addError("活动不存在!");
            return responseBean;
        }

        Page<TpActivitySignUp> page = new Page<>();
        page.setCurrent(null == pageNum ? 0 : pageNum);
        page.setSize(null == pageSize ? 10 : pageSize);
        page.setDesc("sign_up_time");

        IPage<TpActivitySignUp> signUpIPage = tpActivitySignUpMapper.listQuery(page, userName, phone, activityId);

        List<TpActivitySignUp> tpActivitySignUpList = signUpIPage.getRecords();
        // 数据包装填充
        List<ActivitySignUpVO> activitySignUpVOS = tpActivitySignUpList.stream().map(e->{
            ActivitySignUpVO activitySignUpVO = new ActivitySignUpVO();
            BeanTools.copyProperties(e, activitySignUpVO);

            //根据房产ID-去查询用户ID
            TaUserVerify taUserVerify= taUserVerifyMapper.selectById(e.getTaUserId());
//            TaUser taUser = taUserMapper.selectById(taUserVerify.getUserId());

//            activitySignUpVO.setUserName(taUser.getUserName());
//            activitySignUpVO.setPhone(taUser.getLoginName());

            activitySignUpVO.setRoleName(e.getUserRole());
            activitySignUpVO.setAddress(e.getAddress());

            return activitySignUpVO;
        }).collect(Collectors.toList());

        Map<String, Object> map = Maps.newHashMap();
        map.put("list", activitySignUpVOS);
        map.put("total", signUpIPage.getTotal());
        map.put("pageNum", signUpIPage.getCurrent());
        map.put("pageSize", signUpIPage.getSize());

        responseBean.addSuccess(map);
        return responseBean;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseBean deleteSignUp(Integer id, String parameter) {
        ResponseBean responseBean = new ResponseBean();

        JSONObject jsonObject = JSONObject.parseObject(parameter);
        Boolean sendSMS = jsonObject.getBoolean("sendSMS");

        TpActivitySignUp tpActivitySignUp = tpActivitySignUpMapper.selectById(id);
        if (null == tpActivitySignUp) {
            responseBean.addError("该记录不存在!");
            return responseBean;
        }

//        TaUser taUser = taUserMapper.selectById(tpActivitySignUp.getTaUserId());
//        if (null == taUser) {
//            responseBean.addError("APP端 用户不存在!");
//            return responseBean;
//        }

        tpActivitySignUpMapper.deleteById(tpActivitySignUp);
        TpActivity tpActivity = tpActivityMapper.selectById(tpActivitySignUp.getActivityId());
        if (tpActivity.getSignUpCount().intValue() > 0) {
            tpActivity.setSignUpCount(tpActivity.getSignUpCount() - 1);
            tpActivityMapper.updateById(tpActivity);
        }

        // 发送短信
        // TODO 删除成功后, 发送短信通知用户
        if (sendSMS) {
            // 开始发送

        }

        responseBean.addSuccess("操作成功!");
        return responseBean;
    }
}
