package com.sw.mes.yb.pc.service.impl;

import com.sw.mes.core.base.BaseServiceImpl;
import com.sw.mes.core.utils.RequestPage;
import com.sw.mes.base.factory.utils.PageUtils;
import com.sw.mes.yb.pc.dao.PickingInfoDAO;
import com.sw.mes.yb.pc.entity.PickingInfo;
import com.sw.mes.yb.pc.service.PickingInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional
public class PickingInfoServiceImpl extends BaseServiceImpl<PickingInfo> implements PickingInfoService {

    PickingInfoDAO pickingInfoDAO;

    @Autowired
    public void setDao(PickingInfoDAO pickingInfoDAO) {
        this.pickingInfoDAO = pickingInfoDAO;
        super.setBaseDao(pickingInfoDAO);
    }

//    @Autowired
//    private StoreOutputApplyService outputApplyService;

    @Override
    public List<PickingInfo> findByExample(Specification<PickingInfo> specification, RequestPage page) {
        org.springframework.data.domain.Page<PickingInfo> springDataPage = pickingInfoDAO.findAll(specification, PageUtils.createPageable(page));
        page.setTotal(springDataPage.getTotalElements());
        return springDataPage.getContent();
    }

//    @Override
//    public StoreOutputApplyDto initApplyDto(PickingInfo pickingInfo) {
//        StoreOutputApplyDto outputApplyDto = new StoreOutputApplyDto();
//        outputApplyDto.setCode(pickingInfo.getPickingInfoNo());
//        outputApplyDto.setCreateTime(new Date());
//        outputApplyDto.setCreateUserId(BaseContextHandler.getUserID());
//        outputApplyDto.setCreateUserName(BaseContextHandler.getUsername());
//        //出库类型
////        outputApplyDto.setOutputTypeDictId(pickingInfo.getExperimentId());
////        outputApplyDto.setOutputTypeDictName(pickingInfo.getExperimentType());
//        outputApplyDto.setStatus(EnumValues.PickInfoStatus.WAITTING.toString());
//
//        Map<String, Object> infoMap = initJsonString(pickingInfo);
//
//        outputApplyDto.setInfomation(JSONObject.toJSONString(infoMap));
//
//        return outputApplyDto;
//    }

    @Override
    public Map<String, Object> initJsonString(PickingInfo pickingInfo) {
        Map<String, Object> infoMap = new HashMap<>();
        infoMap.put("deviceNo", pickingInfo.getDeviceNo());
        infoMap.put("luciNo", pickingInfo.getLotNo());
        infoMap.put("pickNo", pickingInfo.getPickingInfoNo());
        infoMap.put("use",pickingInfo.getUseDictName());
        infoMap.put("techBom", pickingInfo.getArtBagName());
        infoMap.put("experiment", pickingInfo.getExperimentName());
        infoMap.put("productModelName", pickingInfo.getProductModelName());

        return infoMap;
    }

    @Override
    public void initSaveOrUpdate(PickingInfo pickingInfo) throws Exception {

//        StoreOutputApplyDto outputApplyDto = initApplyDto(pickingInfo);
//
//        List<PickingInfoItem> infoItems = pickingInfo.getInfoItemList();
//        if (infoItems != null) {
//            List<StoreOutputApplyItemDto> itemDtoList = new ArrayList<>();
//            for (PickingInfoItem infoItem: infoItems) {
//                infoItem.setPickingInfo(pickingInfo);
//
//                StoreOutputApplyItemDto itemDto = new StoreOutputApplyItemDto();
//                itemDto.setCount(infoItem.getCount());
//                itemDto.setMaterialModelId(infoItem.getMaterialModelId());
//
//                itemDtoList.add(itemDto);
//            }
//            outputApplyDto.setStoreOutputApplyItemDtos(itemDtoList);
//        }
//
//        try {
//            outputApplyService.pickInfo(outputApplyDto);
//        } catch (Exception e) {
//            e.printStackTrace();
//            throw new Exception(e.getMessage());
//        }
//
//        saveOrUpdate(pickingInfo);
    }

    @Override
    public PickingInfo findByDeviceNo(String deviceNo) {
        return pickingInfoDAO.findByDeviceNo(deviceNo);
    }

    @Override
    public PickingInfo findByLotNo(String lotNo) {
        List<PickingInfo> list = pickingInfoDAO.findByLotNo(lotNo);
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }


}
