package com.lightinit.hsdataportal.impl;

import com.lightinit.hsdataportal.common.OrderNumberUtils;
import com.lightinit.hsdataportal.dictionary.*;
import com.lightinit.hsdataportal.entity.*;
import com.lightinit.hsdataportal.mapper.*;
import com.lightinit.hsdataportal.model.*;
import com.lightinit.hsdataportal.service.IResourceFollowService;
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.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class ResourceFollowServiceImpl implements IResourceFollowService {

    @Autowired
    private ResourceFollowMapper resourceFollowMapper;

    @Autowired
    private ResourceAuthMapper authMapper;

    @Autowired
    private PublishSettingMapper publishSettingMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderGoodsMapper orderGoodsMapper;

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private SectorMapper sectorMapper;

    @Override
    //关注api列表
    public List<MyAPIOpenFollowListModel> queryOpenList(String dicPlatformType, Long followId, BaseExample.Page pager) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        MyAPIFollowSearchModel followSearchModel = new MyAPIFollowSearchModel();
        followSearchModel.setPage(pager);
        followSearchModel.setDicPlatformType(dicPlatformType);
        followSearchModel.setFollowId(followId);
        followSearchModel.setResourceType(DicResourceType.API);
        return resourceFollowMapper.queryOpenList(followSearchModel);
    }

    @Override
    public long queryOpenCount(String dicPlatformType, Long followId) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        MyAPIFollowSearchModel followSearchModel = new MyAPIFollowSearchModel();
        followSearchModel.setDicPlatformType(dicPlatformType);
        followSearchModel.setFollowId(followId);
        followSearchModel.setResourceType(DicResourceType.API);
        return resourceFollowMapper.queryOpenCount(followSearchModel);
    }

    @Override
    //取消关注
    public ResultState<Long> noFollow(Long id) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        ResultState<Long> resultState = new ResultState<Long>();
        int result = resourceFollowMapper.deleteByPrimaryKey(id);
        if (!(result > 0)) {
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
            resultState.setMsg("取消关注失败");
        }
        return resultState;
    }

    @Override
    //查询申请的列表
    public List<MyAPIApplyListModel> applyList(Long followId, String open, BaseExample.Page pager) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        MyAPIApplySearchModel myAPIApplySearchModel = new MyAPIApplySearchModel();
        myAPIApplySearchModel.setDicPlatformType(open);
        myAPIApplySearchModel.setFollowId(followId);
        myAPIApplySearchModel.setPage(pager);
        myAPIApplySearchModel.setResourceType(DicResourceType.API);
        return authMapper.applyList(myAPIApplySearchModel);
    }

    @Override
    public long applyCount(Long followId, String open) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        MyAPIApplySearchModel myAPIApplySearchModel = new MyAPIApplySearchModel();
        myAPIApplySearchModel.setDicPlatformType(open);
        myAPIApplySearchModel.setFollowId(followId);
        myAPIApplySearchModel.setResourceType(DicResourceType.API);
        return authMapper.applyCount(myAPIApplySearchModel);
    }

    @Override
    //申请
    public ResultState<Long> apply(APIApplyModel apiApplyModel, Long followId, String open) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        ResultState<Long> resultState = new ResultState<Long>();
        Date creatAt = new Date();
        int result1 = 0;
        int result2 = 0;
        PublishSetting publishSetting = publishSettingMapper.selectByPrimaryKey(apiApplyModel.getPublishSettingId());
        if (apiApplyModel.getAuditResult().equals(DicResourceAuthAuditResult.AUTHORIZATION_SUCCESS)) {
            //插入order表
            Order order = new Order();
            order.setPlatformType(DicPlatformType.OPEN);
            order.setUserId(followId);
            order.setOrderTime(creatAt);
            order.setPlatformType(OrderNumberUtils.generate());
            order.setOrderState(DicOrderState.PAY_WAIT);
            order.setOrderAmount(publishSetting.getPackagePrice());
            order.setRemark(publishSetting.getDescription());
            order.setCreatedAt(creatAt);
            result1 = orderMapper.insert(order);
            //插入order_goods表
            OrderGoods orderGoods = new OrderGoods();
            BeanUtils.copyProperties(publishSetting, orderGoods);
            orderGoods.setOrderId(order.getId());
            orderGoods.setResourceType(DicResourceType.API);
            orderGoods.setResourceName(apiApplyModel.getResourceName());
            orderGoods.setPublishSettingId(apiApplyModel.getPublishSettingId());
            orderGoods.setCreatedAt(creatAt);
            result2 = orderGoodsMapper.insert(orderGoods);
        }else {
            //修改resource_auth表状态
            ResourceAuth resourceAuth = new ResourceAuth() ;
            resourceAuth.setAuditResult(DicResourceAuthAuditResult.WAIT_AUTHORIZATION);
            ResourceAuthExample example = new ResourceAuthExample() ;
            ResourceAuthExample.Criteria criteria = example.createCriteria() ;
            criteria.andIdEqualTo(apiApplyModel.getId()) ;
            authMapper.updateByExample(resourceAuth,example) ;
        }
        if (!(result1 > 0 && result2 > 0)) {
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
            resultState.setMsg("申请失败");
        }
        return resultState;
    }

    //查看反馈结果
    @Override
    public MyAPIFeedbackModel feedback(Long id) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        MyAPIFeedbackModel myAPIFeedbackModel = new MyAPIFeedbackModel() ;
        ResourceAuth resourceAuth = authMapper.selectByPrimaryKey(id) ;
        if (!StringUtils.isEmpty(resourceAuth)){
            Admin admin = adminMapper.selectByPrimaryKey(resourceAuth.getAuditedBy()) ;
            if (!StringUtils.isEmpty(admin)){
                Sector sector = sectorMapper.selectByPrimaryKey(admin.getOrgId()) ;
                if (!StringUtils.isEmpty(sector)){
                    myAPIFeedbackModel.setAuditedByName(sector.getSectorName());
                }
            }
        }
        myAPIFeedbackModel.setAuthState(resourceAuth.getAuthState());
        myAPIFeedbackModel.setAuditOpinion(resourceAuth.getAuditOpinion()) ;
        return myAPIFeedbackModel;
    }
}
