package com.lightinit.hsdataportal.impl;

import com.lightinit.hsdataportal.entity.Roadshow;
import com.lightinit.hsdataportal.entity.RoadshowRequirement;
import com.lightinit.hsdataportal.entity.RoadshowRequirementWithBLOBs;
import com.lightinit.hsdataportal.entity.RoadshowWithBLOBs;
import com.lightinit.hsdataportal.mapper.RoadshowMapper;
import com.lightinit.hsdataportal.mapper.RoadshowRequirementMapper;
import com.lightinit.hsdataportal.model.ResultState;
import com.lightinit.hsdataportal.model.ResultStateCode;
import com.lightinit.hsdataportal.model.RoadShowSearchModel;
import com.lightinit.hsdataportal.model.RoadshowAddModel;
import com.lightinit.hsdataportal.service.IBusinessIncubationService;
import com.lightinit.hsdataportal.spring.DynamicDataContextHolder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BusinessIncubationServiceImpl implements IBusinessIncubationService {
    @Autowired
    private RoadshowMapper roadshowMapper;
    @Autowired
    private RoadshowRequirementMapper roadshowRequirementMapper;
    @Override
    public List<RoadshowWithBLOBs> getList(RoadShowSearchModel roadShowSearchModel) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return roadshowMapper.getList(roadShowSearchModel);
    }

    @Override
    public long getCount(RoadShowSearchModel roadShowSearchModel) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return roadshowMapper.getCount(roadShowSearchModel);
    }

    @Override
    public ResultState toAdd(RoadshowAddModel roadshowAddModel) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        RoadshowWithBLOBs roadshowWithBLOBs = new RoadshowWithBLOBs();
        BeanUtils.copyProperties(roadshowAddModel,roadshowWithBLOBs);
        ResultState resultState = new ResultState();
        int result = 0;
        result  = roadshowMapper.insert(roadshowWithBLOBs);
         if(!(result>0)) {
             resultState.setMsg("发布路演失败");
             resultState.setStateCode(ResultStateCode.INVALID_DATA);
         }
        resultState.setData(roadshowAddModel.getId());
        return resultState;
    }

    @Override
    public RoadshowWithBLOBs view(Long id) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        RoadshowWithBLOBs roadshowWithBLOBs = roadshowMapper.selectByPrimaryKey(id);
        return roadshowWithBLOBs;
    }

    @Override
    public ResultState toLxwm(RoadshowRequirementWithBLOBs roadshowRequirement) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        ResultState resultState = new ResultState();
        int result = 0;
        result = roadshowRequirementMapper.insert(roadshowRequirement);
        if(!(result>0)) {
            resultState.setMsg("联系我们失败");
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
        }
        resultState.setData(roadshowRequirement.getId());
        return resultState;
    }
}
