package com.jpsoft.shinestar.modules.base.service.impl;

import java.util.*;
import javax.annotation.Resource;

import com.jpsoft.shinestar.modules.base.entity.PersonDeviceRelation;
import com.jpsoft.shinestar.modules.base.entity.PersonInfo;
import com.jpsoft.shinestar.modules.base.service.MessageInfoService;
import com.jpsoft.shinestar.modules.base.service.PersonDeviceRelationService;
import com.jpsoft.shinestar.modules.base.service.PersonInfoService;
import com.jpsoft.shinestar.modules.common.service.BaseService;
import com.jpsoft.shinestar.modules.sys.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import com.jpsoft.shinestar.modules.base.dao.PersonApplicationInfoDAO;
import com.jpsoft.shinestar.modules.base.entity.PersonApplicationInfo;
import com.jpsoft.shinestar.modules.base.service.PersonApplicationInfoService;
import com.github.pagehelper.Page;
import com.jpsoft.shinestar.modules.common.dto.Sort;
import com.github.pagehelper.PageHelper;

@Transactional
@Component(value = "personApplicationInfoService")
@Slf4j
public class PersonApplicationInfoServiceImpl implements PersonApplicationInfoService {
    @Resource(name = "personApplicationInfoDAO")
    private PersonApplicationInfoDAO personApplicationInfoDAO;

    @Autowired
    private PersonInfoService personInfoService;

    @Autowired
	RabbitTemplate rabbitTemplate;

    @Autowired
	private PersonDeviceRelationService personDeviceRelationService;

    @Autowired
    private MessageInfoService messageInfoService;

    @Override
    public PersonApplicationInfo get(String id) {
        // TODO Auto-generated method stub
        return personApplicationInfoDAO.get(id);
    }

    @Override
    public int insert(PersonApplicationInfo model) {
        // TODO Auto-generated method stub
        //model.setId(UUID.randomUUID().toString());

        return personApplicationInfoDAO.insert(model);
    }

    @Override
    public int update(PersonApplicationInfo model) {
        // TODO Auto-generated method stub
        return personApplicationInfoDAO.update(model);
    }

    @Override
    public int delete(String id) {
        // TODO Auto-generated method stub
        return personApplicationInfoDAO.delete(id);
    }

    @Override
    public boolean exist(String id) {
        // TODO Auto-generated method stub
        int count = personApplicationInfoDAO.exist(id);

        return count > 0 ? true : false;
    }

    @Override
    public List<PersonApplicationInfo> list() {
        // TODO Auto-generated method stub
        return personApplicationInfoDAO.list();
    }

    @Override
    public Page<PersonApplicationInfo> pageSearch(Map<String, Object> searchParams, int pageNumber, int pageSize, boolean count, List<Sort> sortList) {
        Page<PersonApplicationInfo> page = PageHelper.startPage(pageNumber, pageSize, count).doSelectPage(() -> {
            personApplicationInfoDAO.search(searchParams, sortList);
        });

        return page;
    }

    @Override
    public void examineApplicationPerson(PersonInfo personInfo, User user, String ids, String status, String deviceIds, String companyId) throws Exception {
        List<String> idList = Arrays.asList(ids.split(","));
        for (String id : idList) {
            PersonApplicationInfo personApplicationInfo = get(id);
            if (!"0".equals(personApplicationInfo.getStatus())) {
                throw new Exception(personApplicationInfo.getName() + "的注册信息已处理过");
            }
            personApplicationInfo.setStatus(status);
            personApplicationInfo.setExamineTime(new Date());
            personApplicationInfo.setDeviceIds(deviceIds);

            if (personInfo != null){
                personApplicationInfo.setExaminePopedomId(personInfo.getPopedom());
                personApplicationInfo.setExamineName(personInfo.getName());

                //非新用户发送站内消息
                if (personApplicationInfo.getPersonId() != null) {
                    //处理通过
                    if ("1".equals(status)) {
                        messageInfoService.sendForPerson(personApplicationInfo.getPersonId(), Long.valueOf(personInfo.getId()), "注册提醒", "注册审批通过！", "1");
                    }
                    //处理拒绝
                    if ("2".equals(status)) {
                        messageInfoService.sendForPerson(personApplicationInfo.getPersonId(), Long.valueOf(personInfo.getId()), "注册提醒", "注册审批拒绝！", "1");
                    }
                }
            }
            else {
                if (user != null){
                    personApplicationInfo.setExaminePopedomId(user.getId());
                    personApplicationInfo.setExamineName(user.getUserName());
                }
            }
            update(personApplicationInfo);







            //审核通过
            if ("1".equals(status)) {
                //处理人员信息写入
                Long personId =  personInfoService.insertPersonAndPopedom(personApplicationInfo, deviceIds,companyId);

                //查询此人是否有人员设备绑定表
				List<PersonDeviceRelation> listRelation = personDeviceRelationService.findByPersonId(personId);

				//消息队列写入人脸信息
				if (listRelation.size() > 0) {
					rabbitTemplate.convertAndSend("importImageExchange", "importImage", personId);
				}

            }
        }
    }


    @Override
    public PersonApplicationInfo findByTelephoneAndStatus(String phone, String status) {
        return personApplicationInfoDAO.findByTelephoneAndStatus(phone, status);
    }

    @Override
    public PersonApplicationInfo findIsExistApply(String companyId, String telephone, String status) {
        return personApplicationInfoDAO.findIsExistApply(companyId, telephone, status);
    }

    @Override
    public PersonApplicationInfo findPhoneAndStatus(String phone, String status) {
        return personApplicationInfoDAO.findPhoneAndStatus(phone, status);
    }
}