package com.weaver.seconddev.chapanda.cost.controller;

import com.weaver.common.authority.annotation.WeaPermission;
import com.weaver.common.base.entity.result.WeaResult;
import com.weaver.seconddev.chapanda.cost.service.ConvertStaffCodeService;
import com.weaver.seconddev.chapanda.cost.service.Request2CostControlService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/sapi/secondev/cbd/workflow")
public class Request2CostControlController {

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

    @Autowired
    Request2CostControlService request2CostControlService;

    @Autowired
    ConvertStaffCodeService convertStaffCodeService;
    @WeaPermission(publicPermission = true)
    @PostMapping("/queryRequestByWorkflowType")
    public WeaResult<Object> queryRequestByWorkflowType(@RequestBody Map<String, Object> params) {
        log.error("queryRequestByWorkflowType");

//        int pageNo2 = 1;
//        int pgeSize2 = 100;
//        boolean noIsNumber = false;
//        if(params.containsKey("pageNo")){
//            String page_no =  String.valueOf(params.get("pageNo"));
//            if(StringUtils.isNotBlank(page_no)){
//                try {
//                    pageNo2 =  Integer.getInteger(page_no);
//                    log.error("pageNo:{}",pageNo2);
//                    noIsNumber =  true;
//                } catch (NumberFormatException e) {
//                    noIsNumber = false;
//                }
//            }
//        }
//
//        boolean sizeIsNumber = false;
//        if(params.containsKey("pageSize")){
//            String page_size =  String.valueOf(params.get("pageSize"));
//            if(StringUtils.isNotBlank(page_size)){
//                try {
//                    pgeSize2 =  Integer.getInteger(page_size);
//                    log.error("pgeSize2:{}",pgeSize2);
//                    sizeIsNumber =  true;
//                } catch (NumberFormatException e) {
//                    sizeIsNumber = false;
//                }
//            }
//        }
//
        int page_no = 1;
        boolean pageIsNumber = false;
        if(params.containsKey("page")){
            String page =  String.valueOf(params.get("page"));
            log.error("page:{}",page);
            if(StringUtils.isNotBlank(page)){
                try {
                    page_no = Integer.valueOf(page);
                    log.error("page_no:{}",page_no);
                    pageIsNumber =  true;
                } catch (NumberFormatException e) {
                    log.error("e:{}",e);
                    pageIsNumber = false;
                }
            }
        }
        log.error("pageIsNumber:{}",pageIsNumber);
        log.error("page_no:{}",page_no);
//
//        log.error("sizeIsNumber:{}",sizeIsNumber);
//        log.error("noIsNumber:{}",noIsNumber);
//        log.error("pageNo2:{}",pageNo2);
//        log.error("pgeSize2:{}",pgeSize2);

        int pageSize = 100;
        Map<String, Object> dataMap = new HashMap<String, Object>();
        String staffcode =  String.valueOf(params.get("staffCode"));
        if(StringUtils.isNotBlank(staffcode)){
            String workflowId =  String.valueOf(params.get("workflowId"));
            if(StringUtils.isNotBlank(workflowId)){
                Long workflowid = Long.valueOf(workflowId);
                Long employeeId = convertStaffCodeService.convEmployeeIdByCode(staffcode);
                if(employeeId > 0){
                    dataMap = request2CostControlService.queryRequestByWorkflowType(params,workflowid,page_no,pageSize,employeeId);
                }else{
                    return WeaResult.fail("工号转换,未获取到员工id");
                }
            }else{
                return WeaResult.fail("流程ID获取为空");
            }
        }else{
            return WeaResult.fail("员工工会stffcode为空");
        }
        return WeaResult.success(dataMap);
    }
}
