package com.lightinit.hsdataplatform.impl.admin;

import com.lightinit.hsdataplatform.common.ObjectMappingUtils;
import com.lightinit.hsdataplatform.entity.*;
import com.lightinit.hsdataplatform.mapper.*;
import com.lightinit.hsdataplatform.model.ResultState;
import com.lightinit.hsdataplatform.model.ResultStateCode;
import com.lightinit.hsdataplatform.model.admin.RequirementMicroappDataModel;
import com.lightinit.hsdataplatform.model.admin.RequirementMicroappPagingSearchModel;
import com.lightinit.hsdataplatform.model.admin.RequirementMicroappSearchModel;
import com.lightinit.hsdataplatform.service.admin.IRequirementMicroappService;
import com.lightinit.hsdataplatform.spring.DynamicDataContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.List;

/**
 * Created by herbert on 2018/5/26.
 */
@Service
public class RequirementMicroappServiceImpl implements IRequirementMicroappService {
    @Autowired
    private RequirementMapper requirementMapper;
    @Autowired
    private RequirementMicroappMapper requirementMicroappMapper;
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ResourceCatelogMapper resourceCatelogMapper;

    public List<Requirement> queryList(RequirementMicroappSearchModel inputModel, BaseExample.Page pager) throws IOException {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        RequirementMicroappPagingSearchModel queryModel = ObjectMappingUtils.map(inputModel, RequirementMicroappPagingSearchModel.class);
        queryModel.setPage(pager);
        return requirementMapper.selectModelList(queryModel);
    }

    public long queryCount(RequirementMicroappSearchModel inputModel) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return requirementMapper.selectModelListCount(inputModel);
    }

    public RequirementMicroappDataModel info(long id){
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
       /* RequirementMicroappDataModel microappDataModel=new RequirementMicroappDataModel();
        Requirement tmpRequirementInfo=requirementMapper.selectByPrimaryKey(id);
        RequirementMicroapp tmpRequirementMicroappInfo=requirementMicroappMapper.selectByPrimaryKey(id);
        User tmpUser=userMapper.selectByPrimaryKey(tmpRequirementInfo.getCreatedBy());
        Admin tmpAdmin=adminMapper.selectByPrimaryKey(tmpRequirementInfo.getAuditedBy());
        ResourceCatelog tmpCatelog=resourceCatelogMapper.selectByPrimaryKey(tmpRequirementMicroappInfo.getCategoryId());
        microappDataModel.setMicroappModel(tmpRequirementMicroappInfo);
        microappDataModel.setRequirmentModel(tmpRequirementInfo);
        if (tmpAdmin!=null){
            microappDataModel.setAuditor(tmpAdmin.getShowname());
        }
        if (tmpUser!=null){
            microappDataModel.setCreateor(tmpUser.getUsername());
        }
        if (tmpCatelog!=null){
            microappDataModel.setCatelogName(tmpCatelog.getCatelogName());
        }
        return  microappDataModel;*/
        RequirementMicroappDataModel dataModel=requirementMapper.getModelDetail(id);
        return dataModel;
    }

    @Transactional
    public ResultState<Long> delete(long id){
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        ResultState<Long> resultState=new ResultState<Long>();

        RequirementMicroappExample example=new RequirementMicroappExample();
        RequirementMicroappExample.Criteria criteria=example.createCriteria();
        criteria.andIdEqualTo(id);
        long sqlResult=requirementMicroappMapper.deleteByExample(example);
        if (sqlResult<=0){
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
            resultState.setMsg("数据不存在");
        }
        resultState.setData(id);
        return  resultState;
    }
}
