package com.sw.mes.fb.turnstore.service.impl;


import com.sw.mes.core.enums.EnumValuesFactory;
import com.sw.mes.core.base.BaseServiceImpl;
import com.sw.mes.fb.checkout.dao.FbJoinDao;
import com.sw.mes.fb.checkout.entity.FbJoin;
import com.sw.mes.fb.checkout.service.FbJoinService;
import com.sw.mes.fb.turnstore.dao.TurnStoreAcceptDao;
import com.sw.mes.fb.turnstore.dto.vo.TurnStoreAcceptVo;
import com.sw.mes.fb.turnstore.entity.TurnStoreAccept;
import com.sw.mes.fb.turnstore.service.TurnStoreAcceptService;
import com.sw.mes.base.workinprocess.service.WipStorageService;
import org.modelmapper.ModelMapper;
import org.modelmapper.convention.MatchingStrategies;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * @Author
 * @Description：
 * @Date: Create in
 * @Modified By:
 */
@Service
@Transactional
public class TurnStoreAcceptServiceImpl extends BaseServiceImpl<TurnStoreAccept> implements TurnStoreAcceptService {
    @Autowired
    ModelMapper modelMapper;

    TurnStoreAcceptDao dao;

    @Autowired
    public void setDao(TurnStoreAcceptDao dao) {
        super.setBaseDao(dao);
        this.dao = dao;
    }

    @Autowired
    private FbJoinDao fbJoinDao;

    @Autowired
    private FbJoinService fbJoinService;

    @Autowired
    private WipStorageService wipStorageService;

    @Override
    public TurnStoreAcceptVo getTurnStoreAcceptById(Long id) {
        TurnStoreAccept turnStoreAccept = dao.getOne(id);
        if (turnStoreAccept != null) {
            String[] ids = turnStoreAccept.getFid().split(",");
            List<FbJoin> list = fbJoinDao.getFbJoinDaoList(ids);
            TurnStoreAcceptVo entity = fromDto(turnStoreAccept);
            entity.setList(list);
            return entity;
        }
        return null;
    }

    @Override
    public void affirmById(Long id, int i, Integer userID, String username, String remark) {
        TurnStoreAccept turnStoreAccept = dao.getOne(id);
        List<FbJoin> list = new ArrayList<>();
        if (turnStoreAccept != null) {
            String[] ids = turnStoreAccept.getFid().split(",");
            list = fbJoinDao.getFbJoinDaoList(ids);
        }
        for (FbJoin fbJoin : list) {
            String[] sections = fbJoin.getSectionNoGroup().split(",");
            for (String section : sections) {
                wipStorageService.selectWipStorage(section, EnumValuesFactory.WipStorageCode.粘方棒.getKey(), true);
                wipStorageService.outStation(section, "B", null);
                fbJoinService.updateIsSend(fbJoin.getId(), 0);
            }
        }
        dao.affirmById(id, i, userID, username, remark, new Date());
    }

    @Override
    public void cancel(Long id, int i, String fid) {
        dao.cancel(id, i);
    }

    @Override
    public void deleteById(Long id, String fid) {
        dao.cancel(id, 3);
    }

    @Override
    public void updateBySid(Long id, int i) {
        dao.updateBySid(id, i);
    }

    @Override
    public void deleteBySendId(Long sendId) {
        dao.deleteBySendId(sendId);
    }

    @Override
    public TurnStoreAccept findBySendId(Long id) {
        return dao.findBySendId(id);
    }

    public TurnStoreAcceptVo fromDto(TurnStoreAccept dto) {
        //ModelMapper modelMapper = new ModelMapper();
        modelMapper.getConfiguration().setFullTypeMatchingRequired(true);
        modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
        return modelMapper.map(dto, TurnStoreAcceptVo.class);
    }
}
