package com.huimv.admin.service.secondary.impl;

import com.huimv.admin.domain.secondary.MachineControlEntity;
import com.huimv.admin.dto.MachineControlDTO;
import com.huimv.admin.repo.secondary.MachineControlRepository;
import com.huimv.admin.service.secondary.MachineControlService;
import com.huimv.admin.vo.MachineControlVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author yinhao
 * @Date 2021/4/1 17:54
 * @Description 料控管理Service
 */
@Service
public class MachineControlServiceImpl implements MachineControlService {

    @Autowired
    private MachineControlRepository machineControlRepository;

//    @Autowired
//    private DaJqdaRepository daJqdaRepository;

//    @Override
//    public Result listByZsId(Integer zsId) {
//        String jqId = daJqdaRepository.findJQIDByZSID(zsId);
//        MachineControlEntity machineControlEntity = machineControlRepository.findByJqid(jqId);
//        return new Result(ResultCode.SUCCESS, machineControlEntity);
//    }

    @Override
    public void save(MachineControlDTO machineControlDTO) {
        boolean isAdd = null == machineControlDTO.getId();
        saveOrUpdateMachineControl(isAdd, machineControlDTO);
    }

    public void saveOrUpdateMachineControl(boolean isAdd, MachineControlDTO machineControlDTO) {

        MachineControlEntity machineControlEntity = new MachineControlEntity();
        machineControlEntity.setNo1DlstartTime1(machineControlDTO.getNo1DlstartTime1());
        machineControlEntity.setNo1DlstartTime2(machineControlDTO.getNo1DlstartTime2());
        machineControlEntity.setNo1DlstartTime3(machineControlDTO.getNo1DlstartTime3());
        machineControlEntity.setNo1DlstartTime4(machineControlDTO.getNo1DlstartTime4());
        machineControlEntity.setNo2DlstartTime1(machineControlDTO.getNo2DlstartTime1());
        machineControlEntity.setNo2DlstartTime2(machineControlDTO.getNo2DlstartTime2());
        machineControlEntity.setNo2DlstartTime3(machineControlDTO.getNo2DlstartTime3());
        machineControlEntity.setNo2DlstartTime4(machineControlDTO.getNo2DlstartTime4());
        machineControlEntity.setNo3DlstartTime1(machineControlDTO.getNo3DlstartTime1());
        machineControlEntity.setNo3DlstartTime2(machineControlDTO.getNo3DlstartTime2());
        machineControlEntity.setNo3DlstartTime3(machineControlDTO.getNo3DlstartTime3());
        machineControlEntity.setNo3DlstartTime4(machineControlDTO.getNo3DlstartTime4());
        machineControlEntity.setNo4DlstartTime1(machineControlDTO.getNo4DlstartTime1());
        machineControlEntity.setNo4DlstartTime2(machineControlDTO.getNo4DlstartTime2());
        machineControlEntity.setNo4DlstartTime3(machineControlDTO.getNo4DlstartTime3());
        machineControlEntity.setNo4DlstartTime4(machineControlDTO.getNo4DlstartTime4());

        if (isAdd) {
            machineControlEntity.setJqId(machineControlDTO.getJqId());
            machineControlRepository.saveAndFlush(machineControlEntity);
            return;
        }

        machineControlEntity.setId(machineControlDTO.getId());
        machineControlRepository.saveAndFlush(machineControlEntity);

    }

    @Override
    public Map<String,Object> list(Integer pageNum, Integer pageSize, String keyword) {
        Map<String,Object> map = new HashMap<>(16);
        Long totalSize = machineControlRepository.countTotalSize(keyword);
        map.put("totalSize",totalSize);
        map.put("totalPageNum",(totalSize + pageSize - 1) / pageSize);
        Integer start = (pageNum - 1) * pageSize;
        List<MachineControlVO> machineControlInfoList = machineControlRepository.getMachineControlInfo(start, pageSize, keyword);
        map.put("data",machineControlInfoList);
        return map;
    }

    @Override
    public void deleteById(Integer id) {
        machineControlRepository.deleteById(id);
    }

    @Override
    public List<String> selectBox4add() {
        return machineControlRepository.selectZsList();
    }


}
