package com.lightinit.hsdataplatform.impl.admin;

import com.lightinit.hsdataplatform.common.PermissionUtilsPro;
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.RequirementMapper;
import com.lightinit.hsdataplatform.mapper.RequirementMicroappMapper;
import com.lightinit.hsdataplatform.mapper.RequirementReceiveMapper;
import com.lightinit.hsdataplatform.model.ResultState;
import com.lightinit.hsdataplatform.model.ResultStateCode;
import com.lightinit.hsdataplatform.model.admin.RequirementMicroappPublishModel;
import com.lightinit.hsdataplatform.model.admin.RequirementReceiveModel;
import com.lightinit.hsdataplatform.service.admin.IRequirementService;
import com.lightinit.hsdataplatform.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;

/**
 * Created by herbert on 2018/5/26.
 */
@Service
public class RequirementServiceImpl implements IRequirementService {
    @Autowired
    private RequirementMapper requirementMapper;
    @Autowired
    private RequirementReceiveMapper requirementReceiveMapper;
    @Autowired
    private RequirementMicroappMapper requirementMicroappMapper;
    @Autowired
    private PermissionUtilsPro permissionUtilsPro;

    public  Requirement info(long id){
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return  requirementMapper.selectByPrimaryKey(id);
    }


    @Transactional
    public ResultState<Long> delete(long id){
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        ResultState<Long> resultState=new ResultState<Long>();
        RequirementExample exmaple=new RequirementExample();
        RequirementExample.Criteria criteria = exmaple.createCriteria();
        criteria.andIdEqualTo(id);
        long sqlQuery=  requirementMapper.deleteByExample(exmaple);
        if ( sqlQuery<=0){
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
            resultState.setMsg("数据不存在");
        }
        resultState.setData(id);

        return  resultState;
    }


    @Transactional
    public ResultState<Long> edit(Requirement auditData){
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        ResultState<Long> resultState=new ResultState<Long>();

        RequirementExample exmaple=new RequirementExample();
        RequirementExample.Criteria criteria = exmaple.createCriteria();
        criteria.andIdEqualTo(auditData.getId());

        long sqlResult=requirementMapper.updateByExample(auditData,exmaple);

        /**Requirement requirement=  requirementMapper.selectByPrimaryKey(auditData.getId());

         String currentUserName=permissionUtilsPro.CurrentName();\
         // Admin admin=adminMapper.selectByUserName(currentUserName);
        requirement.setAuditedAt(new Date());
        requirement.setAuditedBy((long)1);
        requirement.setAuditOpinion(auditData.getAuditedOption());
        requirement.setAuditResult(auditData.getAuditedResult());
         long sqlResult=requirementMapper.updateByPrimaryKey(requirement);
         **/
        if (!(sqlResult > 0)){
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
            resultState.setMsg("执行审核失败");
        }
        resultState.setData(auditData.getId());
        return  resultState;
    }

    @Override
    public ResultState receive(RequirementReceiveModel inputModel) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        ResultState<Long> resultState = new ResultState<Long>();
        inputModel.setCreatedAt(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();
    }


    @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;
    }

}
