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


import com.sw.mes.core.base.BaseServiceImpl;
import com.sw.mes.core.context.BaseContextHandler;
import com.sw.mes.fb.checkout.dao.FbJoinDao;
import com.sw.mes.fb.checkout.entity.FbJoin;
import com.sw.mes.fb.turnstore.dao.TurnStoreSendDao;
import com.sw.mes.fb.turnstore.dto.vo.TurnStoreSenVo;
import com.sw.mes.fb.turnstore.entity.TurnStoreSend;
import com.sw.mes.fb.turnstore.service.TurnStoreSendService;
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.Date;
import java.util.List;


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

    TurnStoreSendDao dao;

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

    @Autowired
    private FbJoinDao fbJoinDao;

    @Autowired
    private WipStorageService wipStorageService;

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

    @Override
    public void updateStateById(Long id, int i, String remark) {
        dao.updateStateById(id, i, remark, BaseContextHandler.getUserID(), BaseContextHandler.getUsername(), new Date());
    }

    @Override
    public void affirmById(Long id, int i, Integer userID,  String username,String remark) {
        dao.affirmById(id, i, userID, username, remark, new Date());
    }

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

    @Override
    public TurnStoreSend save(TurnStoreSend obj) {
        return dao.save(obj);
    }

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