package com.zt.plat.module.qms.business.bus.service;

import cn.hutool.core.collection.CollUtil;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessSampleHandoverPageReqVO;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessSampleHandoverRespVO;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessSampleHandoverSaveReqVO;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.zt.plat.framework.common.pojo.PageResult;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONFactory;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONWriter;
import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessSampleHandoverDO;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessSampleHandoverDetailDO;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessSampleHandoverDetailMapper;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessSampleHandoverMapper;

import static com.zt.plat.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.zt.plat.module.qms.enums.ErrorCodeConstants.*;

/**
 * 样品交接单业务 Service 实现类
 *
 * @author 后台管理
 */
@Service
@Validated
public class BusinessSampleHandoverServiceImpl implements BusinessSampleHandoverService {

    @Resource
    private BusinessSampleHandoverMapper businessSampleHandoverMapper;
    
    @Resource
    private BusinessSampleHandoverDetailMapper businessSampleHandoverDetailMapper;

    @Override
    public BusinessSampleHandoverRespVO createBusinessSampleHandover(BusinessSampleHandoverSaveReqVO createReqVO) {
        // 插入
        BusinessSampleHandoverDO businessSampleHandover = BeanUtils.toBean(createReqVO, BusinessSampleHandoverDO.class);
        businessSampleHandoverMapper.insert(businessSampleHandover);
        // 返回
        return BeanUtils.toBean(businessSampleHandover, BusinessSampleHandoverRespVO.class);
    }

    @Override
    public void updateBusinessSampleHandover(BusinessSampleHandoverSaveReqVO updateReqVO) {
        // 校验存在
        validateBusinessSampleHandoverExists(updateReqVO.getId());
        // 更新
        BusinessSampleHandoverDO updateObj = BeanUtils.toBean(updateReqVO, BusinessSampleHandoverDO.class);
        businessSampleHandoverMapper.updateById(updateObj);
    }

    @Override
    public void deleteBusinessSampleHandover(Long id) {
        // 校验存在
        validateBusinessSampleHandoverExists(id);
        // 删除
        businessSampleHandoverMapper.deleteById(id);
    }

    @Override
        public void deleteBusinessSampleHandoverListByIds(List<Long> ids) {
        // 校验存在
        validateBusinessSampleHandoverExists(ids);
        // 删除
        businessSampleHandoverMapper.deleteByIds(ids);
        }

    private void validateBusinessSampleHandoverExists(List<Long> ids) {
        List<BusinessSampleHandoverDO> list = businessSampleHandoverMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(BUSINESS_SAMPLE_HANDOVER_NOT_EXISTS);
        }
    }

    private void validateBusinessSampleHandoverExists(Long id) {
        if (businessSampleHandoverMapper.selectById(id) == null) {
            throw exception(BUSINESS_SAMPLE_HANDOVER_NOT_EXISTS);
        }
    }

    @Override
    public BusinessSampleHandoverDO getBusinessSampleHandover(Long id) {
        return businessSampleHandoverMapper.selectById(id);
    }

	@Override
	public JSONObject reportDetail(Long id) {
		JSONWriter.Context context = JSONFactory.createWriteContext();
		context.setDateFormat("yyyy-MM-dd HH:mm:ss");
		
		JSONObject result = new JSONObject();
		BusinessSampleHandoverDO businessSampleHandover = businessSampleHandoverMapper.selectById(id);
		
		String jsonString = JSON.toJSONString(businessSampleHandover, context);
		JSONObject businessSampleHandoverJson = JSON.parseObject(jsonString);
		JSONArray jsonArray = new JSONArray();
		jsonArray.add(businessSampleHandoverJson);
		
		List<BusinessSampleHandoverDetailDO> detailList = businessSampleHandoverDetailMapper.selectByBusinessSampleHandoverId(id);
		String detailListJsonString = JSON.toJSONString(detailList, context);
		result.put("main", jsonArray);
		result.put("detail", JSON.parseArray(detailListJsonString));		
		return result;
	}

    @Override
    public PageResult<BusinessSampleHandoverDO> getBusinessSampleHandoverPage(BusinessSampleHandoverPageReqVO pageReqVO) {
        return businessSampleHandoverMapper.selectPage(pageReqVO);
    }

}