package cn.iocoder.yudao.module.system.service.biz;

import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.ElderlyInfoPageReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyInfoDO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyPhotoDO;
import cn.iocoder.yudao.module.system.dal.mysql.biz.ElderlyInfoMapper;
import cn.iocoder.yudao.module.system.dal.mysql.biz.ElderlyPhotoMapper;
import cn.iocoder.yudao.module.system.dal.mysql.tenant.TenantMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import java.util.Date;
import java.util.List;


/**
 * 长者相册 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class ElderlyPhotoServiceImpl implements ElderlyPhotoService {

    @Resource
    private ElderlyPhotoMapper elderlyPhotoMapper;
    @Resource
    private ElderlyInfoMapper elderlyInfoMapper;
    @Resource
    private BuildService buildService;
    @Resource
    private TenantMapper tenantMapper;

    @Override
    public void createPhotos(List<ElderlyPhotoDO> elderlyPhotoList) {
        for (ElderlyPhotoDO elderlyPhotoDO : elderlyPhotoList) {
            elderlyPhotoDO.setCreatedBy(SecurityFrameworkUtils.getLoginUserNickname());
            elderlyPhotoDO.setCreatedTime(new Date());
            elderlyPhotoMapper.insert(elderlyPhotoDO);
        }
    }

    @Override
    public void updatePhotos(List<ElderlyPhotoDO> elderlyPhotoList) {
        for (ElderlyPhotoDO elderlyPhotoDO : elderlyPhotoList) {
            if (elderlyPhotoDO.getId() != null){
                elderlyPhotoMapper.updateById(elderlyPhotoDO);
            }else {
                elderlyPhotoDO.setCreatedBy(SecurityFrameworkUtils.getLoginUserNickname());
                elderlyPhotoDO.setCreatedTime(new Date());
                elderlyPhotoMapper.insert(elderlyPhotoDO);
            }
        }
    }

    @Override
    public void deletePhoto(Long id) {
        elderlyPhotoMapper.deleteById(id);
    }

    @Override
    public List<ElderlyPhotoDO> getPhoto(Long elderId, Integer type) {
        return elderlyPhotoMapper.selectList(new LambdaQueryWrapperX<ElderlyPhotoDO>()
                .eq(ElderlyPhotoDO::getElderId, elderId)
                .eqIfPresent(ElderlyPhotoDO::getType, type));
    }

    @Override
    public PageResult<ElderlyInfoDO> findElderlyPhotoPage(ElderlyInfoPageReqVO pageVO) {
        Page<ElderlyInfoDO> page = new Page<>(pageVO.getPageNo(), pageVO.getPageSize());
        List<ElderlyInfoDO> list = elderlyInfoMapper.findPage(page, pageVO);
        for (ElderlyInfoDO elderlyInfoDO : list) {
            elderlyInfoDO.setBedName(buildService.getFullBedName(elderlyInfoDO.getId()));
            elderlyInfoDO.setTenantName(tenantMapper.selectById(elderlyInfoDO.getTenantId()).getName());
            List<ElderlyPhotoDO> photoList = elderlyPhotoMapper.selectList(new LambdaQueryWrapperX<ElderlyPhotoDO>()
                    .eq(ElderlyPhotoDO::getElderId, elderlyInfoDO.getId())
                    .orderByDesc(ElderlyPhotoDO::getCreatedTime));

            if (CollectionUtil.isNotEmpty(photoList)){
                ElderlyPhotoDO elderlyPhotoDO = photoList.stream().findFirst().get();
                elderlyInfoDO.setPhotoCount(photoList.size());
                elderlyInfoDO.setPhotoLastUpdateDate(elderlyPhotoDO.getCreatedTime());
            }else {
                elderlyInfoDO.setPhotoCount(0);
            }
        }
        return new PageResult<>(list, page.getTotal());
    }
}