package com.weaver.seconddev.chapanda.cost.service.impl;

import com.weaver.common.base.entity.result.WeaResult;
import com.weaver.common.hrm.util.HrmCommonUtil;
import com.weaver.framework.rpc.annotation.RpcReference;
import com.weaver.publishkit.api.util.PublishKitRuntimeUtil;
import com.weaver.seconddev.chapanda.cost.service.Request2CostControlService;
import com.weaver.teams.domain.user.SimpleEmployee;
import com.weaver.workflow.common.constant.list.listdimension.RequestListInitDimensionTabEnum;
import com.weaver.workflow.common.entity.list.api.RequestListConditionApiEntity;
import com.weaver.workflow.common.entity.list.api.publicapi.RequestCountInfoPAEntity;
import com.weaver.workflow.common.entity.list.api.publicapi.RequestListInfoPAEntity;
import com.weaver.workflow.list.api.rest.publicapi.WflRequestListRest;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/***
 * author:shil
 */
@Service
public class Request2CostControlServiceImpl implements Request2CostControlService {

    private final static Logger log = LoggerFactory.getLogger(Request2CostControlServiceImpl.class);

    @RpcReference(group = "workflow")
    WflRequestListRest wflRequestListRest;

    @Autowired
    private HrmCommonUtil hrmCommonUtil;

    @Autowired
    private PublishKitRuntimeUtil publishKitRuntimeUtil;

    /**
     * 
     * @return
     */
    @Override
    public Map<String, Object> queryRequestByWorkflowType(Map<String, Object> params,Long workflowid,int pageNo,int pageSize,Long employeeId) {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        log.error("queryRequestByWorkflowType");
        SimpleEmployee simpleEmployee = hrmCommonUtil.getSimpleEmployee(employeeId);

        log.error("simpleEmployee:{}",simpleEmployee.getId());

        List<Map<String, Object>> requestList = new ArrayList<Map<String, Object>>();

        Long total = 0L;
        try{
            RequestListConditionApiEntity conditionEntity = new RequestListConditionApiEntity();

            List<Long> workflowIdList = new ArrayList<Long>();
            workflowIdList.add(workflowid);
            conditionEntity.setWorkflowIdList(workflowIdList);

            String requestName = String.valueOf(params.get("requestName"));
            String requestmark = String.valueOf(params.get("requestRemark"));
//            String staffCode = String.valueOf(params.get("staffCode"));

            log.error("requestName:{}",requestName);
            log.error("requestmark:{}",requestmark);
//            log.error("staffCode:{}",staffCode);

            conditionEntity.setCreater(employeeId);

            if(!"null".equals(requestName) && StringUtils.isNotBlank(requestName)){
                conditionEntity.setRequestname(requestName);
            }
            if(!"null".equals(requestmark) && StringUtils.isNotBlank(requestmark)){
                conditionEntity.setRequestmark(requestmark);
            }

            WeaResult<RequestCountInfoPAEntity> requestCountInfo = wflRequestListRest.getRequestCountByTabId(simpleEmployee, RequestListInitDimensionTabEnum.DONE_FINISH.getTabid(),conditionEntity);
            int countCode = requestCountInfo.getCode();
            log.error("countCode:{}",countCode);
            if(countCode == 200){
                total = requestCountInfo.getData().getCount();
            }
            log.error("total:{}",total);
            dataMap.put("total",total);

            WeaResult<List<RequestListInfoPAEntity>> requestInfoList = wflRequestListRest.getRequestListByTabId(simpleEmployee, RequestListInitDimensionTabEnum.DONE_FINISH.getTabid(), pageNo, pageSize, conditionEntity);
            int code = requestInfoList.getCode();
            log.error("code:{}",code);
            if(code == 200){
                List<RequestListInfoPAEntity> requestListInfo = requestInfoList.getData();
                log.error("requestListInfo:{}",requestListInfo.size());

                for(int i=0;i<requestListInfo.size();i++){
                    RequestListInfoPAEntity requestListInfoPAEntity = requestListInfo.get(i);
                    String request_Name = requestListInfoPAEntity.getRequestname();
                    String request_remark = requestListInfoPAEntity.getRequestMark();
                    String requestcode = requestListInfoPAEntity.getRequestCode();
                    String appUrl = requestListInfoPAEntity.getAppUrl();
                    String createTime = requestListInfoPAEntity.getCreateTime().toString();
                    String creatorName = requestListInfoPAEntity.getCreatorName();
                    String pcUrl = requestListInfoPAEntity.getPcUrl();

                    Map<String, Object> requestMap = new HashMap<String, Object>();
                    requestMap.put("requestName",request_Name);
                    requestMap.put("requestremark",request_remark);
                    requestMap.put("requestcode",requestcode);
                    requestMap.put("appUrl",appUrl);
                    requestMap.put("createTime",createTime);
                    requestMap.put("creatorName",creatorName);
                    requestMap.put("pcUrl",pcUrl);
                    requestList.add(requestMap);
                }
            }
            dataMap.put("data",requestList);
        }catch (Exception e){
            log.error("e:{}",e.getMessage());
        }
        return dataMap;
    }

}

