package com.lightinit.hsdataportal.impl;

import com.lightinit.hsdataportal.entity.Requirement;
import com.lightinit.hsdataportal.entity.RequirementApiWithBLOBs;
import com.lightinit.hsdataportal.mapper.RequirementApiMapper;
import com.lightinit.hsdataportal.mapper.RequirementMapper;
import com.lightinit.hsdataportal.model.*;
import com.lightinit.hsdataportal.service.IMyApiRequirementService;
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.Date;
import java.util.List;

@Service
public class MyApiRequirementServiceImpl implements IMyApiRequirementService {
    @Autowired
    private RequirementMapper requirementMapper;
    @Autowired
    private RequirementApiMapper requirementApiMapper ;
    @Override
    public List<MyRequirementListModel> selectOpenList(Long id) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return requirementMapper.selectApiList(id);
    }

    @Override
    public ResultState<Long> edit(APIDdmandOpenReleaseModel releaseModel) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        ResultState<Long> resultState = new ResultState<Long>();
        Date createdAt = new Date();
        //编辑requirement
        Requirement requirement = new Requirement() ;
        BeanUtils.copyProperties(releaseModel,requirement);
        int sqlResult1 = requirementMapper.updateByPrimaryKeySelective(requirement) ;

        //编辑requirement_api
        RequirementApiWithBLOBs requirementApiWithBLOBs = new RequirementApiWithBLOBs() ;
        BeanUtils.copyProperties(releaseModel,requirementApiWithBLOBs);
        requirementApiWithBLOBs.setId(requirement.getId());
        int sqlResult2 = requirementApiMapper.updateByPrimaryKeyWithBLOBs(requirementApiWithBLOBs) ;

        if (!(sqlResult1 > 0&&sqlResult2 > 0)){
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
            resultState.setMsg("数据编辑失败");
        }
        resultState.setData(requirement.getId());
        return resultState;
    }
    @Override
    public Requirement selectRequirementById(Long id) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return requirementMapper.selectByPrimaryKey(id);
    }

    @Override
    public ResultState<Long> offLine(Requirement requirement) {
        ResultState<Long> resultState = new ResultState<Long>();
        int sqlResult = requirementMapper.updateByPrimaryKey(requirement);

        if (!(sqlResult > 0)){
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
            resultState.setMsg("下架失败");
        }
        resultState.setData(requirement.getId());
        return resultState;
    }
    @Override
    public List<APIReceiveListModel> selectReceiveList(Long id) {
        return requirementApiMapper.selectReceiveList(id);
    }
}
