package com.goafanti.order.service.impl;


import com.goafanti.common.constant.AFTConstants;
import com.goafanti.common.dao.DepartmentMapper;
import com.goafanti.common.dao.TOrderNewMapper;
import com.goafanti.common.model.Department;
import com.goafanti.core.shiro.token.TokenManager;
import com.goafanti.order.bo.TOrderNewBo;
import com.goafanti.order.enums.ApprovalNewState;
import com.goafanti.order.enums.ProcessStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class PushOrderService {
    public static final String   DEFAULTS_URL="/image/noPermission.jpg";
    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private TOrderNewMapper tOrderNewMapper;



    /**
     * 根据流程与角色判断是否可以查看订单合同
     * @return  超级管理员、运营决策者、总裁、外包审核与可以查看；
     *          营销员与经理可以查看自己的订单；
     *         营销管理员、财务专员、咨询师管理员可以看到在自己手上的合同；其他只能获取到无权限查看的图片
     *
     */
    public String processStatusConstant(TOrderNewBo t){
        //tOrder.getContractPictureUrl(),tOrder.getSalesmanId(),tOrder.getProcessStatus(),tOrder.getApproval()
        //流程状态 0-已派营销员，1-已派营销员管理员，2-已派财务管理员，3-已派财务专员，4-已派咨询师管理员，5-已部分派咨询师经理，6-已部全部派咨询师经理
        if (TokenManager.hasRole(AFTConstants.SUPERADMIN) || TokenManager.hasRole(AFTConstants.APPROVAL_DECISION)
        ||TokenManager.hasRole(AFTConstants.APPROVAL_DECISION_ASSISTANT) ){
            return t.getContractPictureUrl();
        }else if ((TokenManager.hasRole(AFTConstants.SALESMAN_MANAGER)||TokenManager.hasRole(AFTConstants.SALESMAN)
                ||TokenManager.hasRole(AFTConstants.SALESMAN_ASSISTANT))&&
                Objects.equals(TokenManager.getAdminId(), t.getSalesmanId())){
            return  t.getContractPictureUrl();
        }else if (TokenManager.hasRole(AFTConstants.SALESMAN_MANAGER)&& !TokenManager.getAdminId().equals(t.getSalesmanId())){
            if (TokenManager.getAdminId().equals(t.getManagerId())&&t.getProcessStatus()==ProcessStatus.YPYXJL.getCode()){
                return  t.getContractPictureUrl();
            }else {
                return DEFAULTS_URL;
            }
        }else if (TokenManager.hasRole(AFTConstants.SALESMAN_ADMIN)&&t.getProcessStatus()== ProcessStatus.YPYXGLY.getCode()){
            return  t.getContractPictureUrl();
        }else if (TokenManager.hasRole(AFTConstants.FINANCE)&&(t.getProcessStatus()== ProcessStatus.YPCW_JSZJ.getCode())){
            return  t.getContractPictureUrl();
        }else if (TokenManager.hasRole(AFTConstants.OPERATION_GUARANTEE)&&(
                t.getProcessStatus()== ProcessStatus.YYBZSH.getCode()
        )){
            return  t.getContractPictureUrl();
        }else if (TokenManager.hasRole(AFTConstants.TECH_ADMIN)&&(
                t.getProcessStatus()== ProcessStatus.YPCW_JSZJ.getCode()||t.getProcessStatus()== ProcessStatus.YPZXSGLY.getCode()
        )){
            return  t.getContractPictureUrl();
        }else if (TokenManager.hasRole(AFTConstants.OUTSOURCE_AUDITOR)){
            return  t.getContractPictureUrl();
        }
        if(t.getApproval()== ApprovalNewState.TPDSH.getCode()){
            Department department = departmentMapper.selectByPrimaryKey(t.getOrderDep());
            if (department.getApproval()==1&&department.getApprovalAid().equals(TokenManager.getAdminId())){
                return  t.getContractPictureUrl();
            }
        }
            return DEFAULTS_URL;
    }



    /**
     * 根据变更流程判断是否可以查看
     *
     * @param url
     * @param processStatus
     * @param status 在流程走完则要财务退票 状态 0草稿 1审核中 2通过 3驳回 4完成 5撤销 2是流程走完但是还停留在财务退票，4才是完全走完流程
     * @return
     */
    public String changeProcessStatusConstant(String url,String orderNo,Integer processStatus,Integer status){
        //流程状态 0营销员 1营销管理员 2技术员 3技术经理 4技术总监 5财务专员(退单) 6财务总监 7总裁 8董事长 9 财务专员(非退单)
        boolean flag=false;
        //超级管理员、董事长、总裁、总裁助手可以看
        //营销员、营销经理可以看
        if (TokenManager.hasRole(AFTConstants.SUPERADMIN) || TokenManager.hasRole(AFTConstants.APPROVAL_DECISION) ||
                TokenManager.hasRole(AFTConstants.CED) ||TokenManager.hasRole(AFTConstants.CED_ASSISTANT)){
            flag=true;
        } else  if(TokenManager.hasRole(AFTConstants.SALESMAN)||TokenManager.hasRole(AFTConstants.SALESMAN_MANAGER)){
            flag=true;
        }else{
            //其他都是审核得时候可以看

            switch (processStatus) {
                case 2:
                    if(TokenManager.hasRole(AFTConstants.SALESMAN_ADMIN)){
                        flag=true;
                    }
                    break;
                case 3:
                    if(TokenManager.hasRole(AFTConstants.TECH)){
                        flag=true;
                    }
                    break;
                case 4:
                    if(TokenManager.hasRole(AFTConstants.TECH_MANAGER)){
                        flag=true;
                    }
                    break;
                case 5:
                    if(TokenManager.hasRole(AFTConstants.TECH_ADMIN)){
                        flag=true;
                    }
                    break;
                case 6:
                    if(TokenManager.hasRole(AFTConstants.FINANCE)){
                        flag=true;
                    }
                    break;
                case 7:
                    if(TokenManager.hasRole(AFTConstants.FINANCE_ADMIN)){
                        flag=true;
                    }
                    break;
                case 8:
                    if(TokenManager.hasRole(AFTConstants.OPERATION_GUARANTEE)){
                        flag=true;
                    }
                    break;
                case 9:
                    if(status==1){
                        Department department = tOrderNewMapper.selectDepByOrderNo(orderNo);
                        if (TokenManager.getAdminId().equals(department.getApprovalAid())){
                            flag=true;
                        }
                    }
                    if(TokenManager.hasRole(AFTConstants.FINANCE)&&status==2){
                        flag=true;
                    }
                    break;
                case 10:

                    if((TokenManager.hasRole(AFTConstants.APPROVAL_DECISION)||(TokenManager.hasRole(AFTConstants.APPROVAL_DECISION_ASSISTANT))&&status!=2)||
                            (TokenManager.hasRole(AFTConstants.FINANCE)&&status==2)){
                        flag=true;
                    }
                    break;
                case 11:
                    if((TokenManager.hasRole(AFTConstants.VICE_CED))||(TokenManager.hasRole(AFTConstants.FINANCE)&&status==2)){
                        flag=true;
                    }
                    break;
            }
        }
        if (flag){
            return url;
        }else {
            return DEFAULTS_URL;
        }
    }


}
