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

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.elderlyorder.ElderlyServiceOrderPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.elderlyorder.ElderlyServiceOrderRespVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.elderlyorder.ElderlyServiceOrderSaveReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.elderlyorder.excel.ElderlyServiceOrderImportExcelVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyServiceOrderDO;
import cn.iocoder.yudao.module.system.dal.mysql.biz.ElderlyServiceOrderMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

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

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.COMMON_NOT_FOUND;

@Service
@Validated
public class ElderlyServiceOrderServiceImpl implements ElderlyServiceOrderService {

    @Resource
    private ElderlyServiceOrderMapper elderlyServiceOrderMapper;

    @Override
    public Long create(ElderlyServiceOrderSaveReqVO createReqVO) {
        ElderlyServiceOrderDO order = BeanUtils.toBean(createReqVO, ElderlyServiceOrderDO.class);
        elderlyServiceOrderMapper.insert(order);
        return order.getId();
    }

    @Override
    public void update(ElderlyServiceOrderSaveReqVO updateReqVO) {
        validateExists(updateReqVO.getId());
        ElderlyServiceOrderDO updateObj = BeanUtils.toBean(updateReqVO, ElderlyServiceOrderDO.class);
        elderlyServiceOrderMapper.updateById(updateObj);
    }

    @Override
    public void delete(Long id) {
        validateExists(id);
        elderlyServiceOrderMapper.deleteById(id);
    }

    @Override
    public ElderlyServiceOrderDO get(Long id) {
        return elderlyServiceOrderMapper.selectById(id);
    }

    @Override
    public PageResult<ElderlyServiceOrderRespVO> getPage(ElderlyServiceOrderPageReqVO pageReqVO) {
        Page<Object> page = new Page<>(pageReqVO.getPageNo(), pageReqVO.getPageSize());
        List<ElderlyServiceOrderRespVO> list = elderlyServiceOrderMapper.selectElderlyServiceOrderPage(page, pageReqVO);
        return new PageResult<>(list, page.getTotal());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void importExcel(List<ElderlyServiceOrderImportExcelVO> list, Long tenantId) {
        if (list == null || list.isEmpty()) {
            return;
        }
        for (ElderlyServiceOrderImportExcelVO vo : list) {
            ElderlyServiceOrderDO order = new ElderlyServiceOrderDO();
            order.setElderName(vo.getElderName());
            order.setElderSex(vo.getElderSex());
            order.setElderAge(vo.getElderAge());
            order.setElderAddress(vo.getElderAddress());
            order.setIdCard(vo.getIdCard());
            order.setServiceItem(vo.getServiceItem());
            order.setServiceName(vo.getServiceName());
            order.setOrderTime(vo.getOrderTime());
            order.setServiceBeginTime(vo.getServiceBeginTime());
            order.setServiceEndTime(vo.getServiceEndTime());
            order.setServiceHour(vo.getServiceHour());
            order.setIsExpire(vo.getIsExpire());
            order.setOrderStatus(vo.getOrderStatus());
            order.setTenantId(tenantId);
            elderlyServiceOrderMapper.insert(order);
        }
    }

    @Override
    public List<ElderlyServiceOrderRespVO> exportList(ElderlyServiceOrderPageReqVO pageReqVO) {
        // 导出不分页：用一个超大 pageSize（MyBatisPlus Page 只用于插件拼接 limit）
        Page<Object> page = new Page<>(1, Long.MAX_VALUE);
        return elderlyServiceOrderMapper.selectElderlyServiceOrderPage(page, pageReqVO);
    }

    private void validateExists(Long id) {
        if (id == null || elderlyServiceOrderMapper.selectById(id) == null) {
            throw exception(COMMON_NOT_FOUND);
        }
    }
}

