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

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.module.system.controller.admin.home.vo.HomeServiceMemberAssignReqVO;
import cn.iocoder.yudao.module.system.controller.admin.home.vo.HomeServiceMemeberPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.home.vo.HomeServiceMemeberPageRespVO;
import cn.iocoder.yudao.module.system.controller.admin.home.vo.HomeServiceMemeberSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.home.HomeServiceElderRelationDO;
import cn.iocoder.yudao.module.system.dal.dataobject.home.HomeServiceMemberDO;
import cn.iocoder.yudao.module.system.dal.mysql.home.HomeServiceElderRelationMapper;
import cn.iocoder.yudao.module.system.dal.mysql.home.HomeServiceMemberMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Validated
public class HomeServiceMemberServiceImpl implements HomeServiceMemberService {

    @Resource
    private HomeServiceMemberMapper homeServiceMemberMapper;
    @Resource
    private HomeServiceElderRelationMapper homeServiceElderRelationMapper;

    @Override
    public Long createHomeServiceMemeber(HomeServiceMemeberSaveReqVO createReqVO) {
        HomeServiceMemberDO memberDO = BeanUtils.toBean(createReqVO, HomeServiceMemberDO.class);
        homeServiceMemberMapper.insert(memberDO);
        return memberDO.getId();
    }

    @Override
    public void updateHomeServiceMemeber(HomeServiceMemeberSaveReqVO updateReqVO) {
        HomeServiceMemberDO memberDO = BeanUtils.toBean(updateReqVO, HomeServiceMemberDO.class);
        homeServiceMemberMapper.updateById(memberDO);
    }

    @Override
    public void deleteHomeServiceMemeber(Long id) {
        homeServiceMemberMapper.deleteById(id);
    }

    @Override
    public HomeServiceMemeberPageRespVO getHomeServiceMemeber(Long id) {
        return homeServiceMemberMapper.selectWithJoinById(id);
    }

    @Override
    public PageResult<HomeServiceMemeberPageRespVO> getHomeServiceMemeberPage(HomeServiceMemeberPageReqVO pageReqVO) {
        Page<HomeServiceMemeberPageRespVO> page = new Page<>(pageReqVO.getPageNo(), pageReqVO.getPageSize());
        return new PageResult<>(homeServiceMemberMapper.selectPageWithJoin(page, pageReqVO), page.getTotal());
    }

    @Override
    public void assignHomeServiceMember(List<HomeServiceMemberAssignReqVO> assignReqVOList) {
        Long tenantId = TenantContextHolder.getTenantId();
        List<HomeServiceElderRelationDO> relationDOList = new ArrayList<>();
        for (HomeServiceMemberAssignReqVO assignReqVO : assignReqVOList) {
            HomeServiceElderRelationDO relationDO = new HomeServiceElderRelationDO();
            relationDO.setHomeServiceMemberId(assignReqVO.getServiceMemberId());
            relationDO.setHomeElderId(assignReqVO.getHomeElderId());
            relationDO.setTenantId(tenantId);
            relationDOList.add(relationDO);
        }
        homeServiceElderRelationMapper.insertBatch(relationDOList);
    }
}
