package com.lightinit.hsdataportal.impl;

import com.lightinit.hsdataportal.dictionary.*;
import com.lightinit.hsdataportal.entity.*;
import com.lightinit.hsdataportal.mapper.RequirementFollowMapper;
import com.lightinit.hsdataportal.mapper.RequirementMapper;
import com.lightinit.hsdataportal.mapper.RequirementMicroappMapper;
import com.lightinit.hsdataportal.mapper.RequirementReceiveMapper;
import com.lightinit.hsdataportal.model.*;
import com.lightinit.hsdataportal.service.IRequirementMicroappService;
import com.lightinit.hsdataportal.spring.DynamicDataContextHolder;


import org.springframework.beans.BeanUtils;
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;

@Service
public class RequirementMicroappServiceImpl implements IRequirementMicroappService {
    @Autowired
    private RequirementMicroappMapper requirementMicroappMapper;
    @Autowired
    private RequirementMapper requirementMapper;
    @Autowired
    private RequirementFollowMapper requirementFollowMapper;
    @Autowired
    private RequirementReceiveMapper requirementReceiveMapper;

    @Override
    public List<RequirementMicroappListModel> selectOpenList(RequirementMicroappSearchModel inputModel, BaseExample.Page pager) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return requirementMicroappMapper.selectOpenList(inputModel);
    }

    @Override
    public long selectOpenCount(RequirementMicroappSearchModel inputModel) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return requirementMicroappMapper.selectOpenCount(inputModel);
    }

    @Override
    public RequirementMicroappViewModel queryView(long id) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        RequirementMicroappViewModel apiDemandOpenViewModel =requirementMicroappMapper.queryView(id);
        if (apiDemandOpenViewModel.getViewTimes()==null){
            Requirement requirement = new Requirement() ;
            requirement.setId(id);
            requirement.setViewTimes((long) 1);
            requirementMapper.updateByPrimaryKeySelective(requirement) ;
        }else {
            requirementMapper.updateViewTimes(id) ;
        }
        return apiDemandOpenViewModel;
    }

    @Override
    @Transactional
    public ResultState publish(RequirementMicroappPublishModel inputModel) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        ResultState<Long> resultState = new ResultState<Long>();
        Requirement entity = new Requirement();

        BeanUtils.copyProperties(inputModel,entity);
        entity.setRqrmntState(DicRequirmentAuditState.AUDITING);
        entity.setPlatformType(DicPlatformType.OPEN);
        entity.setRqrmntType(DicRqrmntType.MICROAPP);
        entity.setRqrmntSource(DicRqrmntSourceType.OPEN);
        entity.setCreatedAt(new Date());
        int sqlResult1 = requirementMapper.insert(entity);
        RequirementMicroapp entity1 = new RequirementMicroapp();

        BeanUtils.copyProperties(inputModel,entity1);
        entity1.setId(entity.getId());
        int sqlResult2 =requirementMicroappMapper.insert(entity1);
        if(!(sqlResult1>0 && sqlResult2>0)){
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
            resultState.setMsg("数据插入失败");
        }
        resultState.setData(inputModel.getId());
        return resultState;
    }

    @Override
    public ResultState follow(RequirementFollow model) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        ResultState<Long> resultState = new ResultState<Long>();
              int  sqlResult=requirementFollowMapper.insert(model);
        if(!(sqlResult>0)){
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
            resultState.setMsg("关注失败");
        }
        resultState.setData(model.getId());
        return resultState;
    }

    @Override
    public ResultState receive(RequirementReceiveModel inputModel) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        ResultState<Long> resultState = new ResultState<Long>();
        inputModel.setCreatedAt(new Date());
        inputModel.setReceivedAt(new Date());
        RequirementReceive requirementReceive = new RequirementReceive();
        BeanUtils.copyProperties(inputModel,requirementReceive);
              int  sqlResult=requirementReceiveMapper.insert(requirementReceive);
        if(!(sqlResult>0)){
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
            resultState.setMsg("接单失败");
        }
        resultState.setData(inputModel.getId());
        return resultState;
    }

    @Override
    public List<ResourceCatelog> selectCatelog() {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return requirementMicroappMapper.selectCatelog();
    }
}
