package com.lightinit.hsdataportal.impl;

import com.lightinit.hsdataportal.dictionary.DicPlatformType;
import com.lightinit.hsdataportal.dictionary.DicRequirmentAuditState;
import com.lightinit.hsdataportal.dictionary.DicRqrmntSourceType;
import com.lightinit.hsdataportal.dictionary.DicRqrmntType;
import com.lightinit.hsdataportal.entity.*;
import com.lightinit.hsdataportal.mapper.RequirementApiMapper;
import com.lightinit.hsdataportal.mapper.RequirementFollowMapper;
import com.lightinit.hsdataportal.mapper.RequirementMapper;
import com.lightinit.hsdataportal.mapper.RequirementReceiveMapper;
import com.lightinit.hsdataportal.model.*;
import com.lightinit.hsdataportal.service.IAPIDemandService;
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;
import java.util.TimeZone;

@Service
public class APIDemandServiceImpl implements IAPIDemandService {

    @Autowired
    private RequirementApiMapper requirementApiMapper ;

    @Autowired
    private RequirementMapper requirementMapper ;

    @Autowired
    private RequirementFollowMapper followMapper ;

    @Autowired
    private RequirementReceiveMapper requirementReceiveMapper ;

    @Override
    public List<APIDemandOpenListModel> queryOpenList(APIDemandOpenSearchModel demandOpenSearchModel) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return requirementApiMapper.queryOpenList(demandOpenSearchModel);
    }

    @Override
    public long queryOpenCount(APIDemandOpenSearchModel demandOpenSearchModel) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return requirementApiMapper.queryOpenCount(demandOpenSearchModel);
    }

    @Override
    public APIDemandOpenViewModel queryView(Long id) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        APIDemandOpenViewModel apiDemandOpenViewModel =requirementApiMapper.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<Long> release(APIDdmandOpenReleaseModel releaseModel,Long userId) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        ResultState<Long> resultState = new ResultState<Long>();
        Date createdAt = new Date();
        //插入requirement
        Requirement requirement = new Requirement() ;
        BeanUtils.copyProperties(releaseModel,requirement);
        requirement.setCreatedAt(createdAt);
        requirement.setPlatformType(DicPlatformType.OPEN);
        requirement.setRqrmntType(DicRqrmntType.API);
        requirement.setRqrmntState(DicRequirmentAuditState.AUDITING);
        requirement.setRqrmntSource(DicRqrmntSourceType.OPEN);
        requirement.setCreatedBy(userId);
        requirement.setPublishBy(userId);
        int sqlResult1 = requirementMapper.insert(requirement) ;

        //插入requirement_api
        RequirementApiWithBLOBs requirementApiWithBLOBs = new RequirementApiWithBLOBs() ;
        BeanUtils.copyProperties(releaseModel,requirementApiWithBLOBs);
        requirementApiWithBLOBs.setCreatedAt(createdAt);
        requirementApiWithBLOBs.setId(requirement.getId());
        requirementApiWithBLOBs.setCreatedBy(userId);
        int sqlResult2 = requirementApiMapper.insert(requirementApiWithBLOBs) ;

        if (!(sqlResult1 > 0&&sqlResult2 > 0)){
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
            resultState.setMsg("数据插入失败");
        }
        resultState.setData(requirement.getId());
        return resultState;
    }

    @Override
    public int isFollow(Long userId, Long id, String resourceType) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        //查询requirement_follow表
        RequirementFollowExample example = new RequirementFollowExample() ;
        RequirementFollowExample.Criteria criteria = example.createCriteria() ;
        criteria.andFollowerIdEqualTo(userId) ;
        criteria.andRqrmntIdEqualTo(id) ;
        criteria.andPlatformTypeEqualTo(resourceType) ;
        List<RequirementFollow> requirementFollows = followMapper.selectByExample(example) ;
        return requirementFollows.size();
    }

    @Override
    public ResultState follow(Long id, Long userId, String resourceType) {
        TimeZone.setDefault(TimeZone.getTimeZone("CTT"));
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        ResultState<Long> resultState = new ResultState<Long>() ;

        RequirementFollowExample example = new RequirementFollowExample() ;
        RequirementFollowExample.Criteria criteria = example.createCriteria() ;
        criteria.andFollowerIdEqualTo(userId) ;
        criteria.andRqrmntIdEqualTo(id) ;
        criteria.andPlatformTypeEqualTo(resourceType) ;
        List<RequirementFollow> requirementFollows = followMapper.selectByExample(example) ;
        int result = 0 ;int result1 = 0 ;
        if (requirementFollows.size()==0){
            RequirementFollow requirementFollow = new RequirementFollow() ;
            requirementFollow.setPlatformType(resourceType);
            requirementFollow.setFollowerId(userId);
            requirementFollow.setRqrmntId(id);
            requirementFollow.setCreatedAt(new Date());
            result = followMapper.insert(requirementFollow) ;
            resultState.setData(requirementFollow.getId());
            resultState.setMsg("关注成功");
        }else {
            result1 = followMapper.deleteByExample(example) ;
            resultState.setMsg("取消关注");
        }
        if (!(result>0||result1>0)){
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
            resultState.setMsg("关注失败");
        }
        return resultState;
    }

    @Override
    public int isReceive(Long id) {
        RequirementReceiveExample example = new RequirementReceiveExample() ;
        RequirementReceiveExample.Criteria criteria = example.createCriteria() ;
        criteria.andRqrmntIdEqualTo(id) ;
        List<RequirementReceive> requirementReceives = requirementReceiveMapper.selectByExample(example) ;
        return requirementReceives.size();
    }

    @Override
    public List<APIDemandOpenListModel> getMore(APIDemandOpenSearchModel searchModel, int pageNo) {
        BaseExample.Page page = new BaseExample.Page();
        page.setPageSize(5); //默认再显示五条数据
        page.setPageNo(pageNo);
        searchModel.setPage(page);
        return requirementApiMapper.queryOpenList(searchModel);
    }
}
