/*
 * Author: Zev
 * 生成ServiceImpl
 */
package com.tofly.gpsboot.service.sign.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.tofly.api.DepartmentClient;
import com.tofly.api.UserClent;
import com.tofly.common.oauth.util.SecurityUtils;
import com.tofly.gpsboot.common.system.SignFlagEnum;
import com.tofly.gpsboot.common.system.SignTypeEnum;
import com.tofly.pojo.plan.dto.PdasignhisDto;
import com.tofly.pojo.plan.vo.PdasignhisVo;
import com.tofly.gpsboot.mapper.plan.PdasignhisMapper;
import com.tofly.pojo.User;
import com.tofly.pojo.plan.Pdasignhis;
import com.tofly.gpsboot.service.sign.PdasignhisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;


/**
 * 上下班签到历史信息表：TF_YWPN_PDASIGNHIS_W
 *
 * @author admin
 * @date Thu Mar 18 00:00:00 CST 2021
 */
@Service("pdasignhisService")
public class PdasignhisServiceImpl extends ServiceImpl<PdasignhisMapper, Pdasignhis> implements PdasignhisService {

    @Autowired
    private UserClent userClent;
    @Autowired
    private DepartmentClient departmentClient;
    @Override
    public List<PdasignhisVo> viewConversion(List<Pdasignhis> infoList) {
        List<PdasignhisVo> infoVoList = new ArrayList<>();
        infoList.forEach(info -> {
            PdasignhisVo baseInfo = JSONObject.parseObject(JSON.toJSONString(info), PdasignhisVo.class);
            // 增加签到用户名称 转换签到类型/签到标识
            Long signUser = info.getSignUser();
            if (null != signUser && 0 != signUser) {
                User userAudit = userClent.getById(signUser).getResult();
                if(null!=userAudit){
                    baseInfo.setSignUserName(userAudit.getRealName());
                    baseInfo.setDeptId(userAudit.getDepartmentId());
                    baseInfo.setDeptName(userAudit.getDepartmentName());
                    baseInfo.setAvatar(userAudit.getAvatar());
                }

            }

            baseInfo.setSignType(SignTypeEnum.getCodeForValue(info.getSignType()));
            baseInfo.setFlag(SignFlagEnum.getCodeForValue(info.getFlag()));

            infoVoList.add(baseInfo);
        });
        return infoVoList;
    }

    @Override
    public Page<PdasignhisVo> getPdaSignHisPage(Page page, PdasignhisDto parDto) {
        // 当前部门下用户才有权限
        Long departmentId = SecurityUtils.getUser().getDepartmentId();
        Set<Long> userIdList = departmentClient.getDeptUserList(String.valueOf(departmentId)).getResult().stream().map(User::getId).collect(Collectors.toSet());
        Date signStartTime = parDto.getSignStartTime();
        Date signEndTime = parDto.getSignEndTime();
        Pdasignhis pdasign = JSONObject.parseObject(JSON.toJSONString(parDto), Pdasignhis.class);
        QueryWrapper<Pdasignhis> queryWrapper = new QueryWrapper<>();
        if (null != signStartTime && null != signEndTime) {
            queryWrapper.ge("SIGN_TIME",signStartTime);
            queryWrapper.lt("SIGN_TIME",signEndTime);
        }
        Long signUser = pdasign.getSignUser();
        if(null==signUser){
            queryWrapper.in("SIGN_USER",userIdList);
        }

        queryWrapper.setEntity(pdasign);
        queryWrapper.orderByDesc("CREATE_TIME");
        Page infoPageList = this.page(page, queryWrapper);
        List list = this.viewConversion(infoPageList.getRecords());
        infoPageList.setSearchCount(false);
        infoPageList.setTotal(list.size());
        infoPageList.setRecords(list);
        return infoPageList;
    }
}
