package com.xueyi.system.digitalmans.manager.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xueyi.common.core.constant.basic.SqlConstants;
import com.xueyi.common.web.entity.manager.impl.BaseManagerImpl;
import com.xueyi.system.api.digitalmans.domain.dto.DmDigitalmanWorktimeDto;
import com.xueyi.system.api.digitalmans.domain.dto.DmSyncDigitalmanDto;
import com.xueyi.system.api.digitalmans.domain.po.DmDigitalmanExtPo;
import com.xueyi.system.api.digitalmans.domain.po.DmDigitalmanPo;
import com.xueyi.system.api.digitalmans.domain.po.DmModelPo;
import com.xueyi.system.api.organize.domain.vo.SysDeptExt;
import com.xueyi.system.api.resource.domain.po.DmResourcesPo;
import com.xueyi.system.digitalmans.domain.dto.DmDigitalmanDto;
import com.xueyi.system.digitalmans.domain.model.DmDigitalmanConverter;
import com.xueyi.system.digitalmans.domain.model.DmDigitalmanWorktimeConverter;
import com.xueyi.system.digitalmans.domain.query.DmDigitalmanQuery;
import com.xueyi.system.digitalmans.manager.IDmDigitalmanManager;
import com.xueyi.system.digitalmans.mapper.DmDigitalmanExtMapper;
import com.xueyi.system.digitalmans.mapper.DmDigitalmanMapper;
import com.xueyi.system.digitalmans.mapper.DmDigitalmanWorktimeMapper;
import com.xueyi.system.digitalmans.mapper.DmModelMapper;
import com.xueyi.system.organize.mapper.SysDeptExtMapper;
import com.xueyi.system.resource.mapper.DmResourcesMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.Serializable;

/**
 * 数字人基础管理 数据封装层处理
 *
 * @author xueyi
 */
@Component
public class DmDigitalmanManager extends BaseManagerImpl<DmDigitalmanQuery, DmDigitalmanDto, DmDigitalmanPo, DmDigitalmanMapper, DmDigitalmanConverter> implements IDmDigitalmanManager {
    @Autowired
    DmModelMapper modelMapper;

    @Autowired
    DmDigitalmanExtMapper digitalmanExtMapper;

    @Autowired
    DmResourcesMapper dmResourcesMapper;

    @Autowired
    SysDeptExtMapper sysDeptExtMapper;

    @Autowired
    DmDigitalmanWorktimeMapper digitalmanWorktimeMapper;

    @Autowired
    DmDigitalmanWorktimeConverter digitalmanWorktimeConverter;

//    @Override
//    public List<DmDigitalmanDto> selectList(DmDigitalmanQuery query) {
//        List<DmDigitalmanDto> list = super.selectList(query);
//        list.forEach(item->{
//            DmDigitalmanExtPo depo = digitalmanExtMapper.selectOne(Wrappers.<DmDigitalmanExtPo>query().lambda().
//                    eq(DmDigitalmanExtPo::getManCode,item.getManCode()));
//            if (depo !=null) {
//                item.setDeviceId(depo.getDeviceId());
//                item.setSetSpace(depo.getSetSpace());
//            }
//        });
//
//        return list;
//    }

    @Override
    public DmDigitalmanDto selectById(Serializable id) {
        DmDigitalmanDto dto = super.selectById(id);
        //获取模型数据
        DmModelPo po = modelMapper.selectOne(
                Wrappers.<DmModelPo>query().lambda().
                        eq(DmModelPo::getManId,dto.getManCode()));
        if (po != null) {
            dto.setModelUrl(dmResourcesMapper.selectById(po.getResourceId()).getUrl());
        }
        DmDigitalmanExtPo depo = digitalmanExtMapper.selectOne(Wrappers.<DmDigitalmanExtPo>query().lambda().
                eq(DmDigitalmanExtPo::getManCode,dto.getManCode()));
        if (depo != null) {
            dto.setDeviceId(depo.getDeviceId());
            dto.setSetSpace(depo.getSetSpace());
        }

        return dto;
    }

    @Override
    public int update(DmDigitalmanDto dto) {
        //获取ext
        DmDigitalmanExtPo po = digitalmanExtMapper.selectOne(Wrappers.<DmDigitalmanExtPo>query()
                .lambda().eq(DmDigitalmanExtPo::getManCode,dto.getManCode()));

        po.setDeptId(dto.getDeptId());
        if (dto.getSetSpace() != null && dto.getSetSpace() != "") {
            po.setSetSpace(dto.getSetSpace());
        }
        digitalmanExtMapper.updateById(po);

        return super.update(dto);
    }

    @Override
    public DmDigitalmanWorktimeDto getWorkTimeInfo(Serializable id) {
        return digitalmanWorktimeConverter.mapperDto(digitalmanWorktimeMapper.selectById(id));
    }

    @Override
    public int worktimeUpdate(DmDigitalmanWorktimeDto dto) {
        return digitalmanWorktimeMapper.updateById(dto);
    }

    @Override
    public DmSyncDigitalmanDto syncDigitalmanInfo(String manCode, String timestamp) {
        System.out.println("======syncDigitalmanInfo: manCode：" + manCode);
        DmDigitalmanPo dmpo = mapperPo(super.baseMapper.selectByCode(manCode));

        DmDigitalmanExtPo extPo = digitalmanExtMapper.selectByManCode(manCode);
        DmSyncDigitalmanDto sdmdto = new DmSyncDigitalmanDto();
        System.err.println(extPo);
        System.err.println(extPo.getDeptId());
        if (null != extPo && null != extPo.getDeptId()) {
            SysDeptExt deptExt = sysDeptExtMapper.selectWorkTimeByDeptId(extPo.getDeptId());
            sdmdto.setWorkdayHour(deptExt);//考勤时间
        }
        sdmdto.setId(dmpo.getId());
        sdmdto.setManCode(dmpo.getManCode());
        sdmdto.setName(dmpo.getName());
        sdmdto.setNickName(dmpo.getNickName());
        sdmdto.setUpdateTime(dmpo.getUpdateTime());
        sdmdto.setWorktime(digitalmanWorktimeMapper.selectById(dmpo.getId()));
        DmModelPo mpo = modelMapper.selectOne(Wrappers.<DmModelPo>query().lambda().eq(DmModelPo::getManId,dmpo.getId())
                .last(SqlConstants.LIMIT_ONE));
        System.out.println(mpo.getCurrentBackground());
        System.out.println(mpo.getIconId());
        System.out.println(mpo.getCurrentScreenOff());
        sdmdto.setBackground(dmResourcesMapper.selectById(mpo.getCurrentBackground()));
        sdmdto.setIcon(dmResourcesMapper.selectById(mpo.getIconId()));
        sdmdto.setIconPos(mpo.getIconPos());
        DmResourcesPo screenOffPo = dmResourcesMapper.selectById(mpo.getCurrentScreenOff());
        sdmdto.setScreenOff(screenOffPo);
        System.out.println(sdmdto);
        return sdmdto;
    }
}