package com.yx.face.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yx.face.boot.restful.RestDTO;
import com.yx.face.boot.restful.RestResponse;
import com.yx.face.boot.restful.RestResult;
import com.yx.face.boot.restful.ServiceException;
import com.yx.face.boot.uitls.SMSOrIdCardUtils;
import com.yx.face.dao.UserControlEarlyWarningListDao;
import com.yx.face.dao.UserControlEarlyWarningListLogDao;
import com.yx.face.model.dto.UserControlEarlyWarningDTO;
import com.yx.face.model.dto.UserWhitelistDTO;
import com.yx.face.model.entity.UserControlEarlyWarningList;
import com.yx.face.model.entity.UserControlEarlyWarningListLog;
import com.yx.face.model.excel.UserControlEarlyWarningExcel;
import com.yx.face.model.excel.UserWhitelistExcel;
import com.yx.face.model.search.UserControlEarlyWarningDelete;
import com.yx.face.model.search.UserControlEarlyWarningSearch;
import com.yx.face.model.vo.AdminAndUserEarlyWarningVO;
import com.yx.face.model.vo.AdminVO;
import com.yx.face.model.vo.FaceLogVO;
import com.yx.face.model.vo.UserControlEarlyWarningVO;
import com.yx.face.service.AdminService;
import com.yx.face.service.UserControlEarlyWarningListService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 预警人员 服务实现类
 * </p>
 *
 * @author wxg
 * @since 2022-01-24
 */
@Service
public class UserControlEarlyWarningListServiceImpl implements UserControlEarlyWarningListService {

    @Resource
    private UserControlEarlyWarningListDao userControlEarlyWarningListDao;
    @Resource
    private UserControlEarlyWarningListLogDao userControlEarlyWarningListLogDao;

    @Autowired
    private AdminService adminService;

    @Override
    public Integer addOrUpdate(UserControlEarlyWarningDTO userControlEarlyWarningDTO) {
        if (!SMSOrIdCardUtils.isIdCard(userControlEarlyWarningDTO.getEarlyWarningIdNumber()))
            throw new ServiceException("请输入正确的预警人员身份证号");

        Date date = new Date();
        UserControlEarlyWarningList ucewl = new UserControlEarlyWarningList();
        BeanUtils.copyProperties(userControlEarlyWarningDTO, ucewl);
        ucewl.setUpdateTime(date);
        if (userControlEarlyWarningDTO.getId() == null) {
            UserControlEarlyWarningList u = new UserControlEarlyWarningList();
            u.setEarlyWarningIdNumber(userControlEarlyWarningDTO.getEarlyWarningIdNumber());
            u.setAdminId(userControlEarlyWarningDTO.getAdminId());
            UserControlEarlyWarningList userControlEarlyWarningList = userControlEarlyWarningListDao.selectOne(u);
            if (userControlEarlyWarningList != null) throw new ServiceException("该预警人员已经添加到该账号下了");

            ucewl.setCreateTime(date);
            return userControlEarlyWarningListDao.insertSelective(ucewl);
        } else {
//            UserControlEarlyWarningList userControlEarlyWarningList = userControlEarlyWarningListDao.selectByPrimaryKey(userControlEarlyWarningDTO.getId());
//            if(!userControlEarlyWarningList.getEarlyWarningIdNumber().equals(userControlEarlyWarningDTO.getEarlyWarningIdNumber()))
//                throw new ServiceException("预警人员身份照号不可修改");
            ucewl.setId(userControlEarlyWarningDTO.getId());
            return userControlEarlyWarningListDao.updateByPrimaryKeySelective(ucewl);
        }
    }

    @Override
    public RestResult<List<String>> userControlEarlyWarningListExcel(List<Object> datas, Integer adminId) {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < datas.size(); i++) {
            try {
                UserControlEarlyWarningExcel ucewe = (UserControlEarlyWarningExcel) datas.get(i);
                if (ucewe.getEarlyWarningIdNumber() == null)
                    throw new ServiceException("第" + (i + 2) + "行,预警人员身份证号不能为空");

                UserControlEarlyWarningDTO userControlEarlyWarningDTO = new UserControlEarlyWarningDTO();
                BeanUtils.copyProperties(ucewe, userControlEarlyWarningDTO);
                userControlEarlyWarningDTO.setAdminId(adminId);
                try {
                    this.addOrUpdate(userControlEarlyWarningDTO);
                } catch (Exception e) {
                    throw new ServiceException("第" + (i + 2) + "行," + e.getMessage());
                }
            } catch (ServiceException e) {
                list.add(e.getMessage());
                continue;
            }
        }
        datas.clear();//结束后销毁不用的资源
        return RestResponse.ok(list);
    }

    @Override
    public Integer delete(Integer controlEarlyWarningId) {
        return userControlEarlyWarningListDao.deleteByPrimaryKey(controlEarlyWarningId);
    }

    @Override
    public PageInfo<UserControlEarlyWarningVO> getPageList(RestDTO<UserControlEarlyWarningSearch> dto) {
        AdminVO info = adminService.getInfo();
        Map<String, Object> map = new HashMap<>();
        map.put("adminVO", info);
        map.put("query", dto.getData());

        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        List<UserControlEarlyWarningVO> list = userControlEarlyWarningListDao.getPageList(map);
        return new PageInfo<>(list);
    }

    /**
     * 插入匹配人员预警信息
     *
     * @param deviceSn 设备编号
     * @param idNumber 身份证
     * @param faceLogId  进出记录id
     */
    @Async
    @Override
    public void addWarningInformation(String deviceSn, String idNumber, Long faceLogId) {
        // null 代表未查到
        AdminAndUserEarlyWarningVO adminAndUserEarlyWarningVO = userControlEarlyWarningListDao.getAdminWarningInformation(deviceSn, idNumber, 1);
        if (adminAndUserEarlyWarningVO != null) {
            System.out.println(adminAndUserEarlyWarningVO);
            UserControlEarlyWarningListLog userControlEarlyWarningListLog = new UserControlEarlyWarningListLog();
            Date date = new Date();
            userControlEarlyWarningListLog.setUpdateTime(date);
            userControlEarlyWarningListLog.setCreateTime(date);
            userControlEarlyWarningListLog.setControlUnit(adminAndUserEarlyWarningVO.getControlUnit());
            userControlEarlyWarningListLog.setControlPhone(adminAndUserEarlyWarningVO.getControlPhone());
            userControlEarlyWarningListLog.setControlName(adminAndUserEarlyWarningVO.getControlName());
            userControlEarlyWarningListLog.setAdminId(adminAndUserEarlyWarningVO.getAdminId());
            userControlEarlyWarningListLog.setStatus(1);
            userControlEarlyWarningListLog.setFaceLogId(faceLogId);
            System.out.println(userControlEarlyWarningListLog);
            userControlEarlyWarningListLogDao.insert(userControlEarlyWarningListLog);
        }

    }

    @Override
    public List<UserControlEarlyWarningVO> getControlUnitList() {
        AdminVO info = adminService.getInfo();
        Map<String, Object> map = new HashMap<>();
        map.put("adminVO", info);
        List<UserControlEarlyWarningVO> list =  userControlEarlyWarningListDao.getControlUnitList(map);
        return list;
    }

    @Override
    public Integer batchDelete(UserControlEarlyWarningDelete userControlEarlyWarningDelete) {
        Integer deleteNum = userControlEarlyWarningListDao.batchDelete(userControlEarlyWarningDelete);
        return deleteNum;
    }
}
