package com.lightinit.hsdataplatform.impl.admin;


import com.lightinit.hsdataplatform.dictionary.DicPlatformType;
import com.lightinit.hsdataplatform.dictionary.DicRequirmentAuditState;
import com.lightinit.hsdataplatform.dictionary.DicRqrmntSourceType;
import com.lightinit.hsdataplatform.dictionary.DicRqrmntType;
import com.lightinit.hsdataplatform.entity.*;
import com.lightinit.hsdataplatform.mapper.AdminMapper;
import com.lightinit.hsdataplatform.mapper.RequirementApiMapper;
import com.lightinit.hsdataplatform.mapper.RequirementMapper;
import com.lightinit.hsdataplatform.mapper.ResourceCatelogMapper;
import com.lightinit.hsdataplatform.model.ResultState;
import com.lightinit.hsdataplatform.model.ResultStateCode;
import com.lightinit.hsdataplatform.model.admin.APIDdmandOpenReleaseModel;
import com.lightinit.hsdataplatform.model.admin.DataSupplyApiInputModel;
import com.lightinit.hsdataplatform.service.admin.IDataSupplyApiService;
import com.lightinit.hsdataplatform.spring.DynamicDataContextHolder;
import org.apache.commons.lang.StringUtils;
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 DataSupplyApiServiceImpl implements IDataSupplyApiService {

    @Autowired
    private RequirementMapper requirementMapper;
    @Autowired
    private RequirementApiMapper requirementApiMapper;
    @Autowired
    private ResourceCatelogMapper resourceCatelogMapper;
    @Autowired
    private AdminMapper adminMapper;

    @Override
    public RequirementApi QueryRequirementApi(long id) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return requirementApiMapper.selectByPrimaryKey(id);
    }

    @Override
    public Requirement QueryRequirement(long id) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return requirementMapper.selectByPrimaryKey(id);
    }
    @Override
    public List<Requirement> QueryRequirementList(DataSupplyApiInputModel inputModel, BaseExample.Page pager) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        BaseExample example = new BaseExample();
        if (inputModel != null) {
            String whereClause = "rqrmnt_type='api' ";
            if (StringUtils.isNotEmpty(inputModel.getSupplyApiName())) {
                whereClause += "AND rqrmnt_name LIKE '%" + inputModel.getSupplyApiName() + "%'";
            }
            if (StringUtils.isNotEmpty(inputModel.getSupplyApiState()) && !inputModel.getSupplyApiState().equals("全部")) {
                whereClause += " AND rqrmnt_state = '" + inputModel.getSupplyApiState() + "'";
            }
            if (StringUtils.isNotEmpty(inputModel.getApiPublishTime())) {
                whereClause += " AND Date(audited_at) =Date('" + inputModel.getApiPublishTime() + "')";
            }

            whereClause += "order by created_at desc";

            if (StringUtils.isNotEmpty(whereClause)) {
                example.setWhereClause(whereClause);
            }
        }
        if (pager.getPageNo() > 0) {
            pager.setPageNo(pager.getPageNo());
            pager.setPageSize(10);
            example.setPage(pager);
        }
        return requirementMapper.selectByBaseExample(example);
    }

    @Override
    public long QueryRequirementListCount(DataSupplyApiInputModel inputModel) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        BaseExample example = new BaseExample();
        if (inputModel != null) {
            String whereClause = "rqrmnt_type='api' ";
            if (StringUtils.isNotEmpty(inputModel.getSupplyApiName())) {
                whereClause += " AND rqrmnt_name LIKE '%" + inputModel.getSupplyApiName() + "%'";
            }
            if (StringUtils.isNotEmpty(inputModel.getSupplyApiState()) && !inputModel.getSupplyApiState().equals("全部")) {
                whereClause += " AND rqrmnt_state = '" + inputModel.getSupplyApiState() + "'";
            }
            if (StringUtils.isNotEmpty(inputModel.getApiPublishTime())) {
                whereClause += " AND Date(audited_at) =Date('" + inputModel.getApiPublishTime() + "')";
            }
            if (StringUtils.isNotEmpty(whereClause)) {
                example.setWhereClause(whereClause);
            }
        }
        return requirementMapper.countByBaseExample(example);
    }

    @Override
    public Integer UpDateRequirement(Requirement requirement) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return requirementMapper.updateByPrimaryKey(requirement);
    }

    @Override
    public ResourceCatelog QueryCateType(long id){
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return resourceCatelogMapper.selectByPrimaryKey(id);
    }

    @Override
    public Admin QueryAdmin(long id){
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return adminMapper.selectByPrimaryKey(id);
    }
    @Override
    public Admin QueryAdminByUserName(String username){
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return adminMapper.selectByUserName(username);
    }

    @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.BACKSTAGE);
        requirement.setRqrmntType(DicRqrmntType.API);
        requirement.setRqrmntState(DicRequirmentAuditState.AUDITING);
        requirement.setRqrmntSource(DicRqrmntSourceType.BACKSTAGE);
        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.setAttachment(releaseModel.getAttachmentAdd());
        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;
    }

}
